Skip to content

Commit

Permalink
Make existence of manifest optional when performing lifecycle wrappin…
Browse files Browse the repository at this point in the history
…g - fixes #1747 (#1787)
  • Loading branch information
Sebastian McKenzie authored and bestander committed Nov 11, 2016
1 parent b825123 commit 0c17423
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
6 changes: 6 additions & 0 deletions __tests__/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ test.concurrent('install with arg that has binaries', (): Promise<void> => {
return runAdd({}, ['react-native-cli'], 'install-with-arg-and-bin');
});

test.concurrent('add with no manifest creates blank manifest', (): Promise<void> => {
return runAdd({}, ['lodash'], 'add-with-no-manifest', async (config) => {
assert.ok(await fs.exists(path.join(config.cwd, 'package.json')));
});
});

test.concurrent('add should ignore cache', (): Promise<void> => {
// [email protected] gets installed without --save
// [email protected] gets installed with --save
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export default class Config {
if (manifest) {
return manifest;
} else {
throw new MessageError(this.reporter.lang('couldntFindPackagejson', dir));
throw new MessageError(this.reporter.lang('couldntFindPackagejson', dir), 'ENOENT');
}
}

Expand Down
44 changes: 23 additions & 21 deletions src/util/execute-lifecycle-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,29 @@ async function makeEnv(stage: string, cwd: string, config: Config): {
env.npm_config_argv = JSON.stringify({remain:[], cooked: [config.commandName], original: [config.commandName]});

// add npm_package_*
const manifest = await config.readManifest(cwd);
const queue = [['', manifest]];
while (queue.length) {
const [key, val] = queue.pop();
if (key[0] === '_') {
continue;
}

if (typeof val === 'object') {
for (const subKey in val) {
const completeKey = [key, subKey]
.filter((part: ?string): boolean => !!part)
.join('_');
queue.push([completeKey, val[subKey]]);
const manifest = await config.maybeReadManifest(cwd);
if (manifest) {
const queue = [['', manifest]];
while (queue.length) {
const [key, val] = queue.pop();
if (key[0] === '_') {
continue;
}
} else if (IGNORE_MANIFEST_KEYS.indexOf(key) < 0) {
let cleanVal = String(val);
if (cleanVal.indexOf('\n') >= 0) {
cleanVal = JSON.stringify(cleanVal);

if (typeof val === 'object') {
for (const subKey in val) {
const completeKey = [key, subKey]
.filter((part: ?string): boolean => !!part)
.join('_');
queue.push([completeKey, val[subKey]]);
}
} else if (IGNORE_MANIFEST_KEYS.indexOf(key) < 0) {
let cleanVal = String(val);
if (cleanVal.indexOf('\n') >= 0) {
cleanVal = JSON.stringify(cleanVal);
}
env[`npm_package_${key}`] = cleanVal;
}
env[`npm_package_${key}`] = cleanVal;
}
}

Expand Down Expand Up @@ -158,8 +160,8 @@ export async function executeLifecycleScript(
export default executeLifecycleScript;

export async function execFromManifest(config: Config, commandName: string, cwd: string): Promise<void> {
const pkg = await config.readManifest(cwd);
if (!pkg.scripts) {
const pkg = await config.maybeReadManifest(cwd);
if (!pkg || !pkg.scripts) {
return;
}

Expand Down

0 comments on commit 0c17423

Please sign in to comment.