Skip to content

Commit

Permalink
Node 20?
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Dec 10, 2023
1 parent 31f0e07 commit 05783f7
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 55 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const {
isPublish,
autoPublish,
trustedTesters,
} = createConfig(cli.input[0], cli.flags);
} = await createConfig(cli.input[0], cli.flags);

const spinner = ora();
const spinnerStart = text => {
Expand Down
4 changes: 2 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from 'node:process';
import findSource from './find-source.js';

export default function getConfig(command, flags) {
export default async function getConfig(command, flags) {
const apiConfig = {
extensionId: flags.extensionId || process.env.EXTENSION_ID,
clientId: flags.clientId || process.env.CLIENT_ID,
Expand All @@ -11,7 +11,7 @@ export default function getConfig(command, flags) {

return {
apiConfig,
zipPath: findSource(flags.source),
zipPath: await findSource(flags.source),
isUpload: command === 'upload',
isPublish: command === 'publish',
autoPublish: flags.autoPublish,
Expand Down
30 changes: 19 additions & 11 deletions find-source.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import fs from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';

const isZip = filepath => path.extname(filepath) === '.zip';
async function exists(f) {
try {
await fs.stat(f);
return true;
} catch {
return false;
}
}

function processDirectory(resolvedPath, errorStart = 'The') {
if (!fs.existsSync(resolvedPath)) {
async function processDirectory(resolvedPath, errorStart = 'The') {
if (!await exists(resolvedPath)) {
throw new Error(`${errorStart} directory was not found: ${resolvedPath}`);
}

const manifestPath = path.join(resolvedPath, 'manifest.json');
if (!fs.existsSync(manifestPath)) {
if (!await exists(manifestPath)) {
throw new Error(`${errorStart} directory does not contain manifest.json: ${resolvedPath}`);
}

let manifest;
try {
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
if (typeof manifest.manifest_version === 'number') {
return resolvedPath;
}
Expand All @@ -25,15 +33,15 @@ function processDirectory(resolvedPath, errorStart = 'The') {
throw new Error(`${errorStart} directory does not contain a valid manifest.json: ${resolvedPath}`);
}

function getPathFromPackageJson() {
async function getPathFromPackageJson() {
const cwd = process.cwd();

const packageJsonPath = path.join(cwd, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
if (!await exists(packageJsonPath)) {
return undefined;
}

const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const pkg = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
if (!pkg.webExt?.sourceDir) {
return undefined;
}
Expand All @@ -42,7 +50,7 @@ function getPathFromPackageJson() {
return processDirectory(resolvedPath, 'Reading webExt.sourceDir from package.json, the');
}

export default function findSource(flag) {
export default async function findSource(flag) {
const cwd = process.cwd();

if (flag) {
Expand All @@ -51,13 +59,13 @@ export default function findSource(flag) {
return processDirectory(resolvedPath);
}

if (!fs.existsSync(resolvedPath)) {
if (!await exists(resolvedPath)) {
throw new Error(`Zipped extension not found: ${resolvedPath}`);
}

return resolvedPath;
}

return getPathFromPackageJson()
return await getPathFromPackageJson()
?? processDirectory(cwd, 'Using the cwd, the');
}
20 changes: 10 additions & 10 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { mockFileSystem } from './helpers/stubs.js';

mockFileSystem();

test('Favors params over env vars', t => {
test('Favors params over env vars', async t => {
process.env.EXTENSION_ID = 123;
const expectedId = 456;
const config = createConfig(null, { extensionId: expectedId });
const config = await createConfig(null, { extensionId: expectedId });

t.is(config.apiConfig.extensionId, expectedId);
delete process.env.EXTENSION_ID;
});

test('All options supported as env vars', t => {
test('All options supported as env vars', async t => {
const vars = [
'EXTENSION_ID',
'CLIENT_ID',
Expand All @@ -27,7 +27,7 @@ test('All options supported as env vars', t => {
process.env[name] = varsValue;
}

const config = createConfig(null, {});
const config = await createConfig(null, {});
t.is(config.apiConfig.extensionId, varsValue);
t.is(config.apiConfig.clientId, varsValue);
t.is(config.apiConfig.clientSecret, varsValue);
Expand All @@ -38,22 +38,22 @@ test('All options supported as env vars', t => {
}
});

test('Upload', t => {
const config = createConfig('upload', {});
test('Upload', async t => {
const config = await createConfig('upload', {});

t.true(config.isUpload);
t.false(config.isPublish);
});

test('Publish', t => {
const config = createConfig('publish', {});
test('Publish', async t => {
const config = await createConfig('publish', {});

t.true(config.isPublish);
t.false(config.isUpload);
});

test('Auto Publish', t => {
const config = createConfig(null, { autoPublish: true });
test('Auto Publish', async t => {
const config = await createConfig(null, { autoPublish: true });

t.true(config.autoPublish);
});
60 changes: 30 additions & 30 deletions test/find-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,71 @@ import { mockFileSystem } from './helpers/stubs.js';

mockFileSystem();

test('Uses the provided source zip', t => {
test.serial('Uses the provided source zip', async t => {
const source = 'extension.zip';
const result = findSource(source);
const result = await findSource(source);

t.is(result, path.resolve(source));
});

test('Uses the provided source directory', t => {
test.serial('Uses the provided source directory', async t => {
const source = 'extension-dir';
const result = findSource(source);
const result = await findSource(source);

t.is(result, path.resolve(source));
});

test('Uses the source directory from package.json', t => {
const result = findSource();
test.serial('Uses the source directory from package.json', async t => {
const result = await findSource();

t.is(result, path.resolve('extension-dir'));
});

test('Throws if the provided source zip does not exist', t => {
test.serial('Throws if the provided source zip does not exist', async t => {
const source = 'missing.zip';
const error = t.throws(() => findSource(source));

t.is(error.message, `Zipped extension not found: ${path.resolve(source)}`);
await t.throwsAsync(async () => findSource(source), {
message: `Zipped extension not found: ${path.resolve(source)}`,
});
});

test('Throws if the provided source directory does not exist', t => {
test.serial('Throws if the provided source directory does not exist', async t => {
const source = 'missing-dir';
const error = t.throws(() => findSource(source));

t.is(error.message, `The directory was not found: ${path.resolve(source)}`);
await t.throwsAsync(async () => findSource(source), {
message: `The directory was not found: ${path.resolve(source)}`,
});
});

test('Throws if the provided source directory does not contain a manifest.json', t => {
test.serial('Throws if the provided source directory does not contain a manifest.json', async t => {
const source = 'empty-dir';
const error = t.throws(() => findSource(source));

t.is(error.message, `The directory does not contain manifest.json: ${path.resolve(source)}`);
await t.throwsAsync(async () => findSource(source), {
message: `The directory does not contain manifest.json: ${path.resolve(source)}`,
});
});

test('Throws if the provided source directory does not contain a valid manifest.json', t => {
test.serial('Throws if the provided source directory does not contain a valid manifest.json', async t => {
const source = 'extension-dir-but-invalid-manifest';
const error = t.throws(() => findSource(source));

t.is(error.message, `The directory does not contain a valid manifest.json: ${path.resolve(source)}`);
await t.throwsAsync(async () => findSource(source), {
message: `The directory does not contain a valid manifest.json: ${path.resolve(source)}`,
});
});

test('Throws if the source directory from package.json does not exist', t => {
test.serial('Throws if the source directory from package.json does not exist', async t => {
mockFs({
'package.json': '{"webExt": {"sourceDir": "missing-dir"}}',
});

const error = t.throws(() => findSource());

t.is(error.message, `Reading webExt.sourceDir from package.json, the directory was not found: ${path.resolve('missing-dir')}`);
await t.throwsAsync(async () => findSource(), {
message: `Reading webExt.sourceDir from package.json, the directory was not found: ${path.resolve('missing-dir')}`,
});
});

test('Throws if the source directory from package.json does not contain a manifest.json', t => {
test.serial('Throws if the source directory from package.json does not contain a manifest.json', async t => {
mockFs({
'package.json': '{"webExt": {"sourceDir": "empty-dir"}}',
'empty-dir': {},
});

const error = t.throws(() => findSource());

t.is(error.message, `Reading webExt.sourceDir from package.json, the directory does not contain manifest.json: ${path.resolve('empty-dir')}`);
await t.throwsAsync(async () => findSource(), {
message: `Reading webExt.sourceDir from package.json, the directory does not contain manifest.json: ${path.resolve('empty-dir')}`,
});
});
13 changes: 12 additions & 1 deletion test/zipdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ test.serial('Ignores OS junk files', async t => {
resetStub();
});

test.todo('Removes user-supplied root from path in zip');
test('Removes user-supplied root from path in zip', t => {
const zip = new ZipFile();
zip.addFile('./test/fixtures/without-junk/manifest.json', 'manifest.json');
zip.end();

return new Promise(resolve => {
zip.outputStream.on('data', data => {
t.false(data.toString().includes('without-junk'));
resolve();
});
});
});

0 comments on commit 05783f7

Please sign in to comment.