Skip to content

Commit

Permalink
add linting option to sync-dev-deps command
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Sep 30, 2021
1 parent 4c1a5ee commit b6d0c4c
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions packages/addon-shim/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
import { ensureSymlinkSync, readJSONSync, writeJSONSync } from 'fs-extra';
import { join } from 'path';
import yargs from 'yargs/yargs';
import type { Argv } from 'yargs';

function commonArgs(yargs: Argv) {
return yargs
.option('testAppDir', {
type: 'string',
description: 'Path to the test app',
default: 'test-app',
})
.option('addonDir', {
type: 'string',
description: 'Path to your addon',
default: process.cwd(),
});
}

yargs(process.argv.slice(2))
.scriptName('addon-shim')
.command(
'link-test-app',
'Ensures that a test app (that lives a subdir under an addon) has access to the addon and all appropriate deps',
(yargs) => {
return yargs
.option('testAppDir', {
type: 'string',
description: 'Path to the test app',
default: 'test-app',
})
.option('addonDir', {
type: 'string',
description: 'Path to your addon',
default: process.cwd(),
});
},
(yargs) => commonArgs(yargs),
function (opts) {
let { testAppDir, addonDir } = opts;
ensureSymlinkSync(
Expand All @@ -43,35 +46,48 @@ yargs(process.argv.slice(2))
'sync-dev-deps',
`Synchronizes a test app's devDependencies into the parent addon's devDependencies`,
(yargs) => {
return yargs
.option('testAppDir', {
type: 'string',
description: 'Path to the test app',
default: 'test-app',
})
.option('addonDir', {
type: 'string',
description: 'Path to your addon',
default: process.cwd(),
});
return commonArgs(yargs).option('lint', {
type: 'boolean',
description:
'Instead of modifying package.json, print what would have been modified and exit with a failure if any changes are required.',
default: false,
});
},
function (opts) {
let { testAppDir, addonDir } = opts;
let { testAppDir, addonDir, lint } = opts;
let addonPkg = readJSONSync(join(addonDir, 'package.json'));
let testPkg = readJSONSync(join(testAppDir, 'package.json'));
let foundDifferences = false;
let devDeps: { [name: string]: string } = Object.assign(
{},
addonPkg.devDependencies
);
for (let [name, range] of Object.entries(
testPkg.devDependencies as { [name: string]: string }
)) {
if (name !== addonPkg.name) {
devDeps[name] = range;
if (name === addonPkg.name) {
continue;
}
if (devDeps[name] !== range) {
foundDifferences = true;
if (lint) {
console.error(
`test app depends on ${name} ${range} but that is not present in addon's devDependencies package.json`
);
} else {
devDeps[name] = range;
}
}
}
if (!foundDifferences) {
return;
}
if (lint) {
process.exit(-1);
} else {
addonPkg.devDependencies = devDeps;
writeJSONSync(join(addonDir, 'package.json'), addonPkg, { spaces: 2 });
}
addonPkg.devDependencies = devDeps;
writeJSONSync(join(addonDir, 'package.json'), addonPkg, { spaces: 2 });
}
)
.demandCommand()
Expand Down

0 comments on commit b6d0c4c

Please sign in to comment.