Skip to content

Commit 0c17423

Browse files
Sebastian McKenziebestander
authored andcommitted
Make existence of manifest optional when performing lifecycle wrapping - fixes #1747 (#1787)
1 parent b825123 commit 0c17423

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

__tests__/commands/add.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ test.concurrent('install with arg that has binaries', (): Promise<void> => {
7777
return runAdd({}, ['react-native-cli'], 'install-with-arg-and-bin');
7878
});
7979

80+
test.concurrent('add with no manifest creates blank manifest', (): Promise<void> => {
81+
return runAdd({}, ['lodash'], 'add-with-no-manifest', async (config) => {
82+
assert.ok(await fs.exists(path.join(config.cwd, 'package.json')));
83+
});
84+
});
85+
8086
test.concurrent('add should ignore cache', (): Promise<void> => {
8187
// [email protected] gets installed without --save
8288
// [email protected] gets installed with --save

__tests__/fixtures/add/add-with-no-manifest/.gitkeep

Whitespace-only changes.

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export default class Config {
369369
if (manifest) {
370370
return manifest;
371371
} else {
372-
throw new MessageError(this.reporter.lang('couldntFindPackagejson', dir));
372+
throw new MessageError(this.reporter.lang('couldntFindPackagejson', dir), 'ENOENT');
373373
}
374374
}
375375

src/util/execute-lifecycle-script.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,29 @@ async function makeEnv(stage: string, cwd: string, config: Config): {
3737
env.npm_config_argv = JSON.stringify({remain:[], cooked: [config.commandName], original: [config.commandName]});
3838

3939
// add npm_package_*
40-
const manifest = await config.readManifest(cwd);
41-
const queue = [['', manifest]];
42-
while (queue.length) {
43-
const [key, val] = queue.pop();
44-
if (key[0] === '_') {
45-
continue;
46-
}
47-
48-
if (typeof val === 'object') {
49-
for (const subKey in val) {
50-
const completeKey = [key, subKey]
51-
.filter((part: ?string): boolean => !!part)
52-
.join('_');
53-
queue.push([completeKey, val[subKey]]);
40+
const manifest = await config.maybeReadManifest(cwd);
41+
if (manifest) {
42+
const queue = [['', manifest]];
43+
while (queue.length) {
44+
const [key, val] = queue.pop();
45+
if (key[0] === '_') {
46+
continue;
5447
}
55-
} else if (IGNORE_MANIFEST_KEYS.indexOf(key) < 0) {
56-
let cleanVal = String(val);
57-
if (cleanVal.indexOf('\n') >= 0) {
58-
cleanVal = JSON.stringify(cleanVal);
48+
49+
if (typeof val === 'object') {
50+
for (const subKey in val) {
51+
const completeKey = [key, subKey]
52+
.filter((part: ?string): boolean => !!part)
53+
.join('_');
54+
queue.push([completeKey, val[subKey]]);
55+
}
56+
} else if (IGNORE_MANIFEST_KEYS.indexOf(key) < 0) {
57+
let cleanVal = String(val);
58+
if (cleanVal.indexOf('\n') >= 0) {
59+
cleanVal = JSON.stringify(cleanVal);
60+
}
61+
env[`npm_package_${key}`] = cleanVal;
5962
}
60-
env[`npm_package_${key}`] = cleanVal;
6163
}
6264
}
6365

@@ -158,8 +160,8 @@ export async function executeLifecycleScript(
158160
export default executeLifecycleScript;
159161

160162
export async function execFromManifest(config: Config, commandName: string, cwd: string): Promise<void> {
161-
const pkg = await config.readManifest(cwd);
162-
if (!pkg.scripts) {
163+
const pkg = await config.maybeReadManifest(cwd);
164+
if (!pkg || !pkg.scripts) {
163165
return;
164166
}
165167

0 commit comments

Comments
 (0)