Files
projectinfo/ts/projectinfo.classes.npm.ts

39 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-04-18 22:16:46 +02:00
import * as plugins from './projectinfo.plugins.js';
2016-11-26 14:45:56 +01:00
export class ProjectinfoNpm {
2018-09-02 14:21:05 +02:00
isNpm: boolean = false;
packageJson: any = null;
name: string = '';
version: string = '';
status: string = '';
license: string = '';
git: plugins.smartstring.GitRepo | null = null;
2016-02-23 00:58:33 +01:00
private constructor() {}
static async create(cwdArg: string, optionsArg: { gitAccessToken?: string } = {}): Promise<ProjectinfoNpm> {
const instance = new ProjectinfoNpm();
const resolvedCwd = plugins.path.resolve(cwdArg);
const smartFsInstance = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
const packageJsonPath = plugins.path.join(resolvedCwd, 'package.json');
const fileExists = await smartFsInstance.file(packageJsonPath).exists();
if (fileExists) {
instance.isNpm = true;
const content = await smartFsInstance.file(packageJsonPath).encoding('utf8').read();
instance.packageJson = JSON.parse(content as string);
instance.name = instance.packageJson.name;
instance.version = instance.packageJson.version;
instance.status = 'ok';
instance.license = instance.packageJson.license;
if (instance.packageJson.repository) {
instance.git = new plugins.smartstring.GitRepo(
instance.packageJson.repository.url,
2018-09-02 14:21:05 +02:00
optionsArg.gitAccessToken
);
}
}
return instance;
2018-09-02 14:21:05 +02:00
}
2016-11-26 14:45:56 +01:00
}