electron-info
Advanced tools
Comparing version 1.22.4 to 1.22.5
#!/usr/bin/env node | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { program as commander } from 'commander'; | ||
@@ -41,3 +32,3 @@ import { createRequire } from 'module'; | ||
.arguments('[version]') | ||
.action((input) => __awaiter(void 0, void 0, void 0, function* () { | ||
.action(async (input) => { | ||
matchedCommand = true; | ||
@@ -50,6 +41,14 @@ if (!input) { | ||
try { | ||
const electronInfo = new ElectronInfo(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (commanderOptions.debug && { debug: true })), (commanderOptions.force && { forceUpdate: true })), (commanderOptions.latest && { latest: true })), (commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) })), (typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases })), (commanderOptions.source && { releasesUrl: commanderOptions.source })), (commanderOptions.timeout && { timeout: parseInt(commanderOptions.timeout, 10) }))); | ||
const electronInfo = new ElectronInfo({ | ||
...(commanderOptions.debug && { debug: true }), | ||
...(commanderOptions.force && { forceUpdate: true }), | ||
...(commanderOptions.latest && { latest: true }), | ||
...(commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) }), | ||
...(typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases }), | ||
...(commanderOptions.source && { releasesUrl: commanderOptions.source }), | ||
...(commanderOptions.timeout && { timeout: parseInt(commanderOptions.timeout, 10) }), | ||
}); | ||
const releases = commanderOptions.raw | ||
? yield electronInfo.getElectronReleases(input) | ||
: yield electronInfo.getElectronReleases(input, true, commanderOptions.colors); | ||
? await electronInfo.getElectronReleases(input) | ||
: await electronInfo.getElectronReleases(input, true, commanderOptions.colors); | ||
console.info(releases); | ||
@@ -61,3 +60,3 @@ } | ||
} | ||
})); | ||
}); | ||
for (const [dependencyShortName, dependencyFullName] of Object.entries(SupportedDependencies)) { | ||
@@ -69,3 +68,3 @@ commander | ||
.arguments('[version]') | ||
.action((version) => __awaiter(void 0, void 0, void 0, function* () { | ||
.action(async (version) => { | ||
matchedCommand = true; | ||
@@ -78,6 +77,14 @@ if (!version) { | ||
try { | ||
const electronInfo = new ElectronInfo(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (commanderOptions.debug && { debug: true })), (commanderOptions.force && { forceUpdate: true })), (commanderOptions.latest && { latest: true })), (commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) })), (typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases })), (commanderOptions.source && { releasesUrl: commanderOptions.source })), (commanderOptions.timeout && { timeout: commanderOptions.timeout }))); | ||
const electronInfo = new ElectronInfo({ | ||
...(commanderOptions.debug && { debug: true }), | ||
...(commanderOptions.force && { forceUpdate: true }), | ||
...(commanderOptions.latest && { latest: true }), | ||
...(commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) }), | ||
...(typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases }), | ||
...(commanderOptions.source && { releasesUrl: commanderOptions.source }), | ||
...(commanderOptions.timeout && { timeout: commanderOptions.timeout }), | ||
}); | ||
const releases = commanderOptions.raw | ||
? yield electronInfo.getDependencyReleases(dependencyShortName, version) | ||
: yield electronInfo.getDependencyReleases(dependencyShortName, version, true, commanderOptions.colors); | ||
? await electronInfo.getDependencyReleases(dependencyShortName, version) | ||
: await electronInfo.getDependencyReleases(dependencyShortName, version, true, commanderOptions.colors); | ||
console.info(releases); | ||
@@ -89,3 +96,3 @@ } | ||
} | ||
})); | ||
}); | ||
} | ||
@@ -96,9 +103,17 @@ commander | ||
.description('show data for all kinds of releases') | ||
.action(() => __awaiter(void 0, void 0, void 0, function* () { | ||
.action(async () => { | ||
matchedCommand = true; | ||
try { | ||
const electronInfo = new ElectronInfo(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (commanderOptions.debug && { debug: true })), (commanderOptions.force && { forceUpdate: true })), (commanderOptions.latest && { latest: true })), (commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) })), (typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases })), (commanderOptions.source && { releasesUrl: commanderOptions.source })), (commanderOptions.timeout && { timeout: commanderOptions.timeout }))); | ||
const electronInfo = new ElectronInfo({ | ||
...(commanderOptions.debug && { debug: true }), | ||
...(commanderOptions.force && { forceUpdate: true }), | ||
...(commanderOptions.latest && { latest: true }), | ||
...(commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) }), | ||
...(typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases }), | ||
...(commanderOptions.source && { releasesUrl: commanderOptions.source }), | ||
...(commanderOptions.timeout && { timeout: commanderOptions.timeout }), | ||
}); | ||
const releases = commanderOptions.raw | ||
? yield electronInfo.getAllReleases() | ||
: yield electronInfo.getAllReleases(true, commanderOptions.colors); | ||
? await electronInfo.getAllReleases() | ||
: await electronInfo.getAllReleases(true, commanderOptions.colors); | ||
console.info(releases); | ||
@@ -110,3 +125,3 @@ } | ||
} | ||
})); | ||
}); | ||
commander.parse(process.argv); | ||
@@ -113,0 +128,0 @@ if (!commander.args.length || !matchedCommand) { |
@@ -1,10 +0,1 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import chalk from 'chalk'; | ||
@@ -37,3 +28,3 @@ import { format as formatDate } from 'date-fns'; | ||
constructor(options) { | ||
this.options = Object.assign(Object.assign({}, defaultOptions), options); | ||
this.options = { ...defaultOptions, ...options }; | ||
this.options.limit = Math.max(0, this.options.limit); | ||
@@ -50,29 +41,23 @@ this.logger = logdown('electron-info/ElectronInfo', { | ||
} | ||
getAllReleases(formatted, colored) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logger.log('Getting all releases:', { colored, formatted }); | ||
const allReleases = yield this.fileService.getReleases(); | ||
const limitedReleases = this.limitReleases(allReleases); | ||
return formatted ? this.formatReleases(limitedReleases, colored) : limitedReleases; | ||
}); | ||
async getAllReleases(formatted, colored) { | ||
this.logger.log('Getting all releases:', { colored, formatted }); | ||
const allReleases = await this.fileService.getReleases(); | ||
const limitedReleases = this.limitReleases(allReleases); | ||
return formatted ? this.formatReleases(limitedReleases, colored) : limitedReleases; | ||
} | ||
getDependencyReleases(dependency, version, formatted, colored) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logger.log('Getting dependency releases:', { colored, dependency, formatted, version }); | ||
const allReleases = yield this.fileService.getReleases(); | ||
const dependencyVersions = yield this.getVersions(allReleases, dependency, version); | ||
const filteredReleases = allReleases.filter(release => release.deps && dependencyVersions.includes(release.deps[dependency])); | ||
const limitedReleases = this.limitReleases(filteredReleases); | ||
return formatted ? this.formatDependencyReleases(limitedReleases, colored) : limitedReleases; | ||
}); | ||
async getDependencyReleases(dependency, version, formatted, colored) { | ||
this.logger.log('Getting dependency releases:', { colored, dependency, formatted, version }); | ||
const allReleases = await this.fileService.getReleases(); | ||
const dependencyVersions = await this.getVersions(allReleases, dependency, version); | ||
const filteredReleases = allReleases.filter(release => release.deps && dependencyVersions.includes(release.deps[dependency])); | ||
const limitedReleases = this.limitReleases(filteredReleases); | ||
return formatted ? this.formatDependencyReleases(limitedReleases, colored) : limitedReleases; | ||
} | ||
getElectronReleases(version, formatted, colored) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logger.log('Getting Electron releases:', { colored, formatted, version }); | ||
const allReleases = yield this.fileService.getReleases(); | ||
const electronVersions = yield this.getVersions(allReleases, 'electron', version); | ||
const filteredReleases = allReleases.filter(release => electronVersions.includes(release.version)); | ||
const limitedReleases = this.limitReleases(filteredReleases); | ||
return formatted ? this.formatReleases(limitedReleases, colored) : limitedReleases; | ||
}); | ||
async getElectronReleases(version, formatted, colored) { | ||
this.logger.log('Getting Electron releases:', { colored, formatted, version }); | ||
const allReleases = await this.fileService.getReleases(); | ||
const electronVersions = await this.getVersions(allReleases, 'electron', version); | ||
const filteredReleases = allReleases.filter(release => electronVersions.includes(release.version)); | ||
const limitedReleases = this.limitReleases(filteredReleases); | ||
return formatted ? this.formatReleases(limitedReleases, colored) : limitedReleases; | ||
} | ||
@@ -123,39 +108,37 @@ buildFoundString(releases) { | ||
} | ||
getVersions(releases, key, inputVersion) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logger.log('Getting versions:', { inputVersion, key }); | ||
const satisfiesVersion = (dependencyVersion, inputVersion) => { | ||
const dependencyVersionClean = semver.clean(dependencyVersion, { loose: true }) || ''; | ||
return semver.satisfies(dependencyVersionClean, inputVersion, { | ||
includePrerelease: true, | ||
loose: true, | ||
}); | ||
}; | ||
let dependencyVersions = []; | ||
if (!this.options.electronPrereleases) { | ||
const tempReleaseNumber = releases.length; | ||
releases = releases.filter(release => semver.prerelease(release.version) === null); | ||
this.logger.log('Removing electron prereleases from found versions', { | ||
after: releases.length, | ||
before: tempReleaseNumber, | ||
}); | ||
async getVersions(releases, key, inputVersion) { | ||
this.logger.log('Getting versions:', { inputVersion, key }); | ||
const satisfiesVersion = (dependencyVersion, inputVersion) => { | ||
const dependencyVersionClean = semver.clean(dependencyVersion, { loose: true }) || ''; | ||
return semver.satisfies(dependencyVersionClean, inputVersion, { | ||
includePrerelease: true, | ||
loose: true, | ||
}); | ||
}; | ||
let dependencyVersions = []; | ||
if (!this.options.electronPrereleases) { | ||
const tempReleaseNumber = releases.length; | ||
releases = releases.filter(release => semver.prerelease(release.version) === null); | ||
this.logger.log('Removing electron prereleases from found versions', { | ||
after: releases.length, | ||
before: tempReleaseNumber, | ||
}); | ||
} | ||
dependencyVersions = releases | ||
.filter(release => { | ||
if (key !== 'electron' && !release.deps) { | ||
return false; | ||
} | ||
dependencyVersions = releases | ||
.filter(release => { | ||
if (key !== 'electron' && !release.deps) { | ||
return false; | ||
} | ||
if (inputVersion === 'all') { | ||
return true; | ||
} | ||
if (key === 'electron' && release.npm_dist_tags && release.npm_dist_tags.includes(inputVersion)) { | ||
return true; | ||
} | ||
return key === 'electron' | ||
? satisfiesVersion(release.version, inputVersion) | ||
: satisfiesVersion(release.deps[key], inputVersion); | ||
}) | ||
.map(release => (key === 'electron' ? release.version : release.deps[key])); | ||
return dependencyVersions; | ||
}); | ||
if (inputVersion === 'all') { | ||
return true; | ||
} | ||
if (key === 'electron' && release.npm_dist_tags && release.npm_dist_tags.includes(inputVersion)) { | ||
return true; | ||
} | ||
return key === 'electron' | ||
? satisfiesVersion(release.version, inputVersion) | ||
: satisfiesVersion(release.deps[key], inputVersion); | ||
}) | ||
.map(release => (key === 'electron' ? release.version : release.deps[key])); | ||
return dependencyVersions; | ||
} | ||
@@ -162,0 +145,0 @@ limitReleases(releases) { |
@@ -1,10 +0,1 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { expect, describe, test, beforeEach, beforeAll, afterAll, afterEach } from 'vitest'; | ||
@@ -39,11 +30,11 @@ import { StatusCodes as HTTP_STATUS } from 'http-status-codes'; | ||
]; | ||
const provideReleaseFile = () => __awaiter(void 0, void 0, void 0, function* () { | ||
yield fs.copy(fullReleasesFile, path.join(tempDirDownload, 'latest.json')); | ||
}); | ||
const provideReleaseFile = async () => { | ||
await fs.copy(fullReleasesFile, path.join(tempDirDownload, 'latest.json')); | ||
}; | ||
describe('ElectronInfo', () => { | ||
let releases; | ||
beforeAll(() => __awaiter(void 0, void 0, void 0, function* () { | ||
yield fs.ensureDir(tempDir); | ||
releases = yield fs.readFile(fullReleasesFile, 'utf8'); | ||
})); | ||
beforeAll(async () => { | ||
await fs.ensureDir(tempDir); | ||
releases = await fs.readFile(fullReleasesFile, 'utf8'); | ||
}); | ||
beforeEach(() => { | ||
@@ -55,4 +46,4 @@ nock(mockUrl).get('/').reply(HTTP_STATUS.OK, releases); | ||
describe('getElectronReleases', () => { | ||
test('parses Electron versions', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
test('parses Electron versions', async () => { | ||
const result = await new ElectronInfo({ | ||
releasesUrl: mockUrl, | ||
@@ -63,5 +54,5 @@ tempDirectory: tempDir, | ||
expect(result[0].version).toBe('5.0.8'); | ||
})); | ||
test('parses Electron SemVer', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
}); | ||
test('parses Electron SemVer', async () => { | ||
const result = await new ElectronInfo({ | ||
releasesUrl: mockUrl, | ||
@@ -72,5 +63,5 @@ tempDirectory: tempDir, | ||
expect(result.length).toBe(23); | ||
})); | ||
test('parses dist tags', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
}); | ||
test('parses dist tags', async () => { | ||
const result = await new ElectronInfo({ | ||
releasesUrl: mockUrl, | ||
@@ -80,5 +71,5 @@ tempDirectory: tempDir, | ||
expect(result.length).toBe(1); | ||
})); | ||
test('returns nothing for invalid versions', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
}); | ||
test('returns nothing for invalid versions', async () => { | ||
const result = await new ElectronInfo({ | ||
releasesUrl: mockUrl, | ||
@@ -88,9 +79,9 @@ tempDirectory: tempDir, | ||
expect(result.length).toBe(0); | ||
})); | ||
test('forces downloading the release file', () => __awaiter(void 0, void 0, void 0, function* () { | ||
}); | ||
test('forces downloading the release file', async () => { | ||
const customBody = createRandomBody(); | ||
const customUrl = 'http://custom.com'; | ||
yield provideReleaseFile(); | ||
await provideReleaseFile(); | ||
nock(customUrl).get('/').reply(HTTP_STATUS.OK, customBody); | ||
const result = yield new ElectronInfo({ | ||
const result = await new ElectronInfo({ | ||
forceUpdate: true, | ||
@@ -101,7 +92,7 @@ releasesUrl: customUrl, | ||
expect(result).toEqual(customBody); | ||
})); | ||
}); | ||
}); | ||
describe('getDependencyReleases', () => { | ||
test('parses Chrome versions', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
test('parses Chrome versions', async () => { | ||
const result = await new ElectronInfo({ | ||
releasesUrl: mockUrl, | ||
@@ -113,5 +104,5 @@ tempDirectory: tempDir, | ||
expect(result[0].deps.chrome).toBe('71.0.3578.98'); | ||
})); | ||
test('parses Chrome SemVer', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
}); | ||
test('parses Chrome SemVer', async () => { | ||
const result = await new ElectronInfo({ | ||
releasesUrl: mockUrl, | ||
@@ -122,5 +113,5 @@ tempDirectory: tempDir, | ||
expect(result.length).toBe(56); | ||
})); | ||
test('returns nothing for invalid versions', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
}); | ||
test('returns nothing for invalid versions', async () => { | ||
const result = await new ElectronInfo({ | ||
releasesUrl: mockUrl, | ||
@@ -130,6 +121,6 @@ tempDirectory: tempDir, | ||
expect(result.length).toBe(0); | ||
})); | ||
test('limits releases', () => __awaiter(void 0, void 0, void 0, function* () { | ||
}); | ||
test('limits releases', async () => { | ||
const limit = 2; | ||
const result = yield new ElectronInfo({ | ||
const result = await new ElectronInfo({ | ||
limit, | ||
@@ -140,13 +131,13 @@ releasesUrl: mockUrl, | ||
expect(result.length).toBe(limit); | ||
})); | ||
test('uses a local copy of the releases', () => __awaiter(void 0, void 0, void 0, function* () { | ||
}); | ||
test('uses a local copy of the releases', async () => { | ||
nock(invalidUrl).get('/').reply(HTTP_STATUS.NOT_FOUND); | ||
yield provideReleaseFile(); | ||
yield new ElectronInfo({ | ||
await provideReleaseFile(); | ||
await new ElectronInfo({ | ||
releasesUrl: invalidUrl, | ||
tempDirectory: tempDirDownload, | ||
}).getDependencyReleases('chrome', 'all'); | ||
})); | ||
test('uses latest as alias for limit=1', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield new ElectronInfo({ | ||
}); | ||
test('uses latest as alias for limit=1', async () => { | ||
const result = await new ElectronInfo({ | ||
latest: true, | ||
@@ -158,4 +149,4 @@ releasesUrl: mockUrl, | ||
expect(result[0].version).toBe('8.0.0-nightly.20190820'); | ||
})); | ||
}); | ||
}); | ||
}); |
@@ -1,10 +0,1 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { isAfter as isAfterDate, sub as subtractDate } from 'date-fns'; | ||
@@ -33,81 +24,71 @@ import { constants as fsConstants, promises as fs } from 'fs'; | ||
} | ||
getReleases() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logger.log('Parsing releases URL', { releasesUrl: this.options.releasesUrl }); | ||
const parsedUrl = parseUrl(this.options.releasesUrl); | ||
if (!parsedUrl.href) { | ||
throw new Error('Invalid releases URL provided'); | ||
} | ||
if (parsedUrl.protocol === 'file') { | ||
this.logger.log('Releases URL points to a local file:', { releasesUrl: this.options.releasesUrl }); | ||
return this.loadReleasesFile(path.resolve(this.options.releasesUrl)); | ||
} | ||
this.logger.log('Releases URL points to a URL:', { releasesUrl: this.options.releasesUrl }); | ||
const tempDirectory = yield this.createTempDir(); | ||
const tempFile = path.join(tempDirectory, 'latest.json'); | ||
const tempFileExists = yield this.isPathReadable(tempFile); | ||
if (this.options.forceUpdate) { | ||
this.logger.log(`Force download of the releases file requested`, { | ||
forceUpdate: this.options.forceUpdate, | ||
releasesUrl: this.options.releasesUrl, | ||
tempFile, | ||
}); | ||
return this.httpService.downloadReleasesFile(this.options.releasesUrl, tempFile); | ||
} | ||
if (tempFileExists) { | ||
this.logger.log('Found a local copy of the releases file:', { tempFile }); | ||
const tempFileFromToday = yield this.isFileFromToday(tempFile); | ||
this.logger.log(`Releases file "${tempFile}" is less than 24 hours old:`, tempFileFromToday); | ||
if (tempFileFromToday) { | ||
return this.loadReleasesFile(tempFile); | ||
} | ||
} | ||
async getReleases() { | ||
this.logger.log('Parsing releases URL', { releasesUrl: this.options.releasesUrl }); | ||
const parsedUrl = parseUrl(this.options.releasesUrl); | ||
if (!parsedUrl.href) { | ||
throw new Error('Invalid releases URL provided'); | ||
} | ||
if (parsedUrl.protocol === 'file') { | ||
this.logger.log('Releases URL points to a local file:', { releasesUrl: this.options.releasesUrl }); | ||
return this.loadReleasesFile(path.resolve(this.options.releasesUrl)); | ||
} | ||
this.logger.log('Releases URL points to a URL:', { releasesUrl: this.options.releasesUrl }); | ||
const tempDirectory = await this.createTempDir(); | ||
const tempFile = path.join(tempDirectory, 'latest.json'); | ||
const tempFileExists = await this.isPathReadable(tempFile); | ||
if (this.options.forceUpdate) { | ||
this.logger.log(`Force download of the releases file requested`, { | ||
forceUpdate: this.options.forceUpdate, | ||
releasesUrl: this.options.releasesUrl, | ||
tempFile, | ||
}); | ||
return this.httpService.downloadReleasesFile(this.options.releasesUrl, tempFile); | ||
}); | ||
} | ||
createTempDir() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const tempDirectory = this.options.tempDirectory || path.join(os.tmpdir(), 'electron-info'); | ||
const tempDirectoryExists = yield this.isPathReadable(tempDirectory); | ||
if (!tempDirectoryExists) { | ||
this.logger.log('Creating temp directory', { tempDirectory }); | ||
yield fs.mkdir(tempDirectory); | ||
} | ||
if (tempFileExists) { | ||
this.logger.log('Found a local copy of the releases file:', { tempFile }); | ||
const tempFileFromToday = await this.isFileFromToday(tempFile); | ||
this.logger.log(`Releases file "${tempFile}" is less than 24 hours old:`, tempFileFromToday); | ||
if (tempFileFromToday) { | ||
return this.loadReleasesFile(tempFile); | ||
} | ||
else { | ||
this.logger.log('Temp directory exists', { tempDirectory }); | ||
} | ||
return tempDirectory; | ||
}); | ||
} | ||
return this.httpService.downloadReleasesFile(this.options.releasesUrl, tempFile); | ||
} | ||
isFileFromToday(fileName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { mtime: fileModifiedDate } = yield fs.stat(fileName); | ||
this.logger.log(`File "${fileName}" is from "${fileModifiedDate.toString()}"`); | ||
const yesterday = subtractDate(new Date(), { days: 1 }); | ||
return isAfterDate(fileModifiedDate, yesterday); | ||
}); | ||
async createTempDir() { | ||
const tempDirectory = this.options.tempDirectory || path.join(os.tmpdir(), 'electron-info'); | ||
const tempDirectoryExists = await this.isPathReadable(tempDirectory); | ||
if (!tempDirectoryExists) { | ||
this.logger.log('Creating temp directory', { tempDirectory }); | ||
await fs.mkdir(tempDirectory); | ||
} | ||
else { | ||
this.logger.log('Temp directory exists', { tempDirectory }); | ||
} | ||
return tempDirectory; | ||
} | ||
isPathReadable(filePath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
yield fs.access(filePath, fsConstants.F_OK | fsConstants.R_OK); | ||
return true; | ||
} | ||
catch (error) { | ||
this.logger.log('File is not readable:', { errorMessage: error.message }); | ||
return false; | ||
} | ||
}); | ||
async isFileFromToday(fileName) { | ||
const { mtime: fileModifiedDate } = await fs.stat(fileName); | ||
this.logger.log(`File "${fileName}" is from "${fileModifiedDate.toString()}"`); | ||
const yesterday = subtractDate(new Date(), { days: 1 }); | ||
return isAfterDate(fileModifiedDate, yesterday); | ||
} | ||
loadReleasesFile(localPath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logger.log('Loading local releases file:', { localPath }); | ||
const rawData = yield fs.readFile(localPath, 'utf8'); | ||
const releases = JSON.parse(rawData); | ||
if (!Array.isArray(releases)) { | ||
throw new Error('Invalid data in releases file'); | ||
} | ||
return releases; | ||
}); | ||
async isPathReadable(filePath) { | ||
try { | ||
await fs.access(filePath, fsConstants.F_OK | fsConstants.R_OK); | ||
return true; | ||
} | ||
catch (error) { | ||
this.logger.log('File is not readable:', { errorMessage: error.message }); | ||
return false; | ||
} | ||
} | ||
async loadReleasesFile(localPath) { | ||
this.logger.log('Loading local releases file:', { localPath }); | ||
const rawData = await fs.readFile(localPath, 'utf8'); | ||
const releases = JSON.parse(rawData); | ||
if (!Array.isArray(releases)) { | ||
throw new Error('Invalid data in releases file'); | ||
} | ||
return releases; | ||
} | ||
} |
@@ -1,10 +0,1 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { promises as fs } from 'fs'; | ||
@@ -26,22 +17,20 @@ import { inspect } from 'util'; | ||
} | ||
downloadReleasesFile(downloadUrl, targetFile) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logger.log('Downloading releases file:', { downloadUrl, targetFile }); | ||
let releases = []; | ||
try { | ||
const response = yield axios.get(downloadUrl, { timeout: this.options.timeout }); | ||
releases = response.data; | ||
} | ||
catch (error) { | ||
throw new Error(`Request failed: "${error.message}"`); | ||
} | ||
// eslint-disable-next-line no-magic-numbers | ||
this.logger.info('Received data from server:', inspect(releases).toString().slice(0, 40), '...'); | ||
if (!Array.isArray(releases)) { | ||
throw new Error('Invalid data received from server'); | ||
} | ||
yield fs.writeFile(targetFile, JSON.stringify(releases)); | ||
return releases; | ||
}); | ||
async downloadReleasesFile(downloadUrl, targetFile) { | ||
this.logger.log('Downloading releases file:', { downloadUrl, targetFile }); | ||
let releases = []; | ||
try { | ||
const response = await axios.get(downloadUrl, { timeout: this.options.timeout }); | ||
releases = response.data; | ||
} | ||
catch (error) { | ||
throw new Error(`Request failed: "${error.message}"`); | ||
} | ||
// eslint-disable-next-line no-magic-numbers | ||
this.logger.info('Received data from server:', inspect(releases).toString().slice(0, 40), '...'); | ||
if (!Array.isArray(releases)) { | ||
throw new Error('Invalid data received from server'); | ||
} | ||
await fs.writeFile(targetFile, JSON.stringify(releases)); | ||
return releases; | ||
} | ||
} |
@@ -1,10 +0,1 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { assert, describe, test, beforeEach, afterEach } from 'vitest'; | ||
@@ -30,10 +21,10 @@ import { StatusCodes as HTTP_STATUS } from 'http-status-codes'; | ||
describe('downloadReleasesFile', () => { | ||
test('honors a custom timeout', () => __awaiter(void 0, void 0, void 0, function* () { | ||
test('honors a custom timeout', async () => { | ||
try { | ||
yield httpService.downloadReleasesFile(mockUrl, ''); | ||
await httpService.downloadReleasesFile(mockUrl, ''); | ||
assert.fail('Should throw on timeout'); | ||
} | ||
catch (error) { } | ||
})); | ||
}); | ||
}); | ||
}); |
@@ -40,3 +40,11 @@ #!/usr/bin/env node | ||
try { | ||
const electronInfo = new ElectronInfo(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (commanderOptions.debug && { debug: true })), (commanderOptions.force && { forceUpdate: true })), (commanderOptions.latest && { latest: true })), (commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) })), (typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases })), (commanderOptions.source && { releasesUrl: commanderOptions.source })), (commanderOptions.timeout && { timeout: parseInt(commanderOptions.timeout, 10) }))); | ||
const electronInfo = new ElectronInfo({ | ||
...(commanderOptions.debug && { debug: true }), | ||
...(commanderOptions.force && { forceUpdate: true }), | ||
...(commanderOptions.latest && { latest: true }), | ||
...(commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) }), | ||
...(typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases }), | ||
...(commanderOptions.source && { releasesUrl: commanderOptions.source }), | ||
...(commanderOptions.timeout && { timeout: parseInt(commanderOptions.timeout, 10) }), | ||
}); | ||
const releases = commanderOptions.raw | ||
@@ -66,3 +74,11 @@ ? await electronInfo.getElectronReleases(input) | ||
try { | ||
const electronInfo = new ElectronInfo(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (commanderOptions.debug && { debug: true })), (commanderOptions.force && { forceUpdate: true })), (commanderOptions.latest && { latest: true })), (commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) })), (typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases })), (commanderOptions.source && { releasesUrl: commanderOptions.source })), (commanderOptions.timeout && { timeout: commanderOptions.timeout }))); | ||
const electronInfo = new ElectronInfo({ | ||
...(commanderOptions.debug && { debug: true }), | ||
...(commanderOptions.force && { forceUpdate: true }), | ||
...(commanderOptions.latest && { latest: true }), | ||
...(commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) }), | ||
...(typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases }), | ||
...(commanderOptions.source && { releasesUrl: commanderOptions.source }), | ||
...(commanderOptions.timeout && { timeout: commanderOptions.timeout }), | ||
}); | ||
const releases = commanderOptions.raw | ||
@@ -86,3 +102,11 @@ ? await electronInfo.getDependencyReleases(dependencyShortName, version) | ||
try { | ||
const electronInfo = new ElectronInfo(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (commanderOptions.debug && { debug: true })), (commanderOptions.force && { forceUpdate: true })), (commanderOptions.latest && { latest: true })), (commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) })), (typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases })), (commanderOptions.source && { releasesUrl: commanderOptions.source })), (commanderOptions.timeout && { timeout: commanderOptions.timeout }))); | ||
const electronInfo = new ElectronInfo({ | ||
...(commanderOptions.debug && { debug: true }), | ||
...(commanderOptions.force && { forceUpdate: true }), | ||
...(commanderOptions.latest && { latest: true }), | ||
...(commanderOptions.limit && { limit: parseInt(commanderOptions.limit, 10) }), | ||
...(typeof commanderOptions.prereleases !== 'undefined' && { electronPrereleases: commanderOptions.prereleases }), | ||
...(commanderOptions.source && { releasesUrl: commanderOptions.source }), | ||
...(commanderOptions.timeout && { timeout: commanderOptions.timeout }), | ||
}); | ||
const releases = commanderOptions.raw | ||
@@ -89,0 +113,0 @@ ? await electronInfo.getAllReleases() |
@@ -28,3 +28,3 @@ import chalk from 'chalk'; | ||
constructor(options) { | ||
this.options = Object.assign(Object.assign({}, defaultOptions), options); | ||
this.options = { ...defaultOptions, ...options }; | ||
this.options.limit = Math.max(0, this.options.limit); | ||
@@ -31,0 +31,0 @@ this.logger = logdown('electron-info/ElectronInfo', { |
@@ -22,3 +22,3 @@ { | ||
"rimraf": "5.0.7", | ||
"typescript": "5.4.5", | ||
"typescript": "5.5.2", | ||
"vitest": "1.6.0" | ||
@@ -62,4 +62,4 @@ }, | ||
"type": "module", | ||
"version": "1.22.4", | ||
"gitHead": "28c184f53a87d8eb082cc27c923ef7b352bbe875" | ||
"version": "1.22.5", | ||
"gitHead": "f7a6a79286e4eb85392b5f2d33942ab166142109" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104669
1342