Skip to content

Commit fb35257

Browse files
author
Chris Brody
authored
cleanup: move internal npmAddScriptSync function (#150)
and finish removing `lib/utils` with zero test updates since this change should *not* affect any actual behavior
1 parent 0383db9 commit fb35257

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

lib/lib.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ const execaDefault = require('execa');
1010
// default fs object
1111
const fsExtra = require('fs-extra');
1212

13+
const jsonfile = require('jsonfile');
14+
1315
// Internal imports:
1416

1517
const normalizedOptions = require('./normalized-options');
1618

17-
// FUTURE TBD remaining module in utils to be factored out:
18-
const { npmAddScriptSync } = require('./utils');
19-
2019
// Imports from templates
2120

2221
const templates = require('../templates');
@@ -46,6 +45,22 @@ const renderTemplateIfValid = (fs, root, template, templateArgs) => {
4645
);
4746
};
4847

48+
// FUTURE TBD make this asynchronous and possibly more functional:
49+
const npmAddScriptSync = (packageJsonPath, script, fs) => {
50+
try {
51+
var packageJson = jsonfile.readFileSync(packageJsonPath, { fs });
52+
if (!packageJson.scripts) packageJson.scripts = {};
53+
packageJson.scripts[script.key] = script.value;
54+
jsonfile.writeFileSync(packageJsonPath, packageJson, { fs, spaces: 2 });
55+
} catch (e) {
56+
if (/ENOENT.*package.json/.test(e.message)) {
57+
throw new Error(`The package.json at path: ${packageJsonPath} does not exist.`);
58+
} else {
59+
throw e;
60+
}
61+
}
62+
};
63+
4964
const generateWithNormalizedOptions = ({
5065
name,
5166
prefix,

lib/utils/index.js

-5
This file was deleted.

lib/utils/npmAddScriptSync.js

-19
This file was deleted.

0 commit comments

Comments
 (0)