| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- import { basename, dirname, relative } from 'path';
- import { createHash } from 'crypto';
- import { exists, readJsonAsync, writeJsonAsync, createReadStream } from 'fs-extra-promise';
- import * as semver from 'semver';
- interface IInstaller {
- arch: string;
- path: string;
- hash: string;
- created: number;
- }
- interface IUpdater {
- arch: string;
- fromVersion: string;
- path: string;
- hash: string;
- created: number;
- }
- interface IVersion {
- version: string;
- changelog: string;
- source: string;
- installers: IInstaller[];
- updaters: IUpdater[];
- }
- interface IData {
- latest: string;
- versions: IVersion[];
- }
- export class NsisVersions {
- protected outputDir: string;
- protected data: IData;
- constructor(protected path: string) {
- this.outputDir = dirname(path);
- }
- public async addVersion(version: string, changelog: string, source: string) {
- const data = await this.getData();
- if(!data.versions.find(item => item.version == version)) {
- data.versions.push({
- version,
- changelog,
- source: basename(source),
- installers: [],
- updaters: [],
- });
- }
- this.updateLatestVersion();
- }
- public async getVersions(): Promise<string[]> {
- const data = await this.getData();
- return data.versions.map(item => item.version);
- }
- public async getVersion(version: string): Promise<IVersion> {
- const data = await this.getData();
- const item = data.versions.find(item => item.version == version);
- if(!item) {
- throw new Error('ERROR_VERSION_NOT_FOUND');
- }
- return item;
- }
- public async addInstaller(version: string, arch: string, path: string) {
- const data = await this.getData();
- const versionItem: IVersion = data.versions.find(item => item.version == version);
- if(!versionItem) {
- throw new Error('ERROR_VERSION_NOT_FOUND');
- }
- if(!versionItem.installers.find(item => item.arch == arch)) {
- versionItem.installers.push({
- arch,
- path: relative(this.outputDir, path),
- hash: await this.hashFile('sha256', path),
- created: Date.now(),
- });
- }
- }
- public async addUpdater(version: string, fromVersion: string, arch: string, path: string) {
- const data = await this.getData();
- const versionItem: IVersion = data.versions.find(item => item.version == version);
- if(!versionItem) {
- throw new Error('ERROR_VERSION_NOT_FOUND');
- }
- if(!versionItem.updaters.find(item => item.fromVersion == fromVersion && item.arch == arch)) {
- versionItem.updaters.push({
- fromVersion,
- arch,
- path: relative(this.outputDir, path),
- hash: await this.hashFile('sha256', path),
- created: Date.now(),
- });
- }
- }
- public async save() {
- await writeJsonAsync(this.path, this.data);
- }
- protected async getData() {
- if(!this.data) {
- this.data = (await new Promise((resolve, reject) => exists(this.path, resolve)))
- ? await readJsonAsync(this.path)
- : {
- latest: undefined,
- versions: [],
- };
- }
- return this.data;
- }
- protected updateLatestVersion() {
- if(this.data.versions.length == 0) {
- return;
- }
- const versions = [ ...this.data.versions ];
- versions.sort((a, b) => semver.gt(a.version, b.version) ? -1 : 1);
- this.data.latest = versions[0].version;
- }
- protected hashFile(type: string, path: string): Promise<string> {
- return new Promise((resolve, reject) => {
- const hasher = createHash(type);
- hasher.on('error', reject);
- hasher.on('readable', () => {
- const data = hasher.read();
- if(data) {
- hasher.end();
- resolve((<any>data).toString('hex'));
- }
- });
- createReadStream(path).pipe(hasher);
- });
- }
- }
|