4 examples of 'how to generate package lock json' in JavaScript

Every line of 'how to generate package lock json' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
3function createPackageLockTree ({physicalTree}) {
4 const logicalNode = physicalTree.logicalNode
5 const fileVersion = /^file:/.test(logicalNode.version)
6 ? logicalNode.version
7 : false
8 const githubVersion = /^git+/.test(logicalNode.resolved)
9 ? logicalNode.resolved
10 : false
11 return {
12 bundled: physicalTree.bundled || undefined,
13 optional: physicalTree.optional || undefined,
14 dev: physicalTree.dev || undefined,
15 version: githubVersion || fileVersion || physicalTree.version,
16 resolved: physicalTree.bundled || githubVersion || fileVersion
17 ? undefined
18 : physicalTree.manifest._resolved,
19 integrity: physicalTree.bundled || githubVersion || fileVersion
20 ? undefined
21 : physicalTree.manifest._integrity,
22 requires: logicalNode.dependencies && logicalNode.dependencies.size > 0
23 ? Array.from(logicalNode.dependencies.keys()).reduce((requires, name) => {
24 const childNode = logicalNode.dependencies.get(name)
25 const version = childNode.manifest.version
26 requires[name] = version
27 return requires
28 }, {})
29 : undefined,
30 dependencies: physicalTree.children.length > 0
31 ? physicalTree.children.reduce((dependencies, physicalChild) => {
32 dependencies[physicalChild.name] = createPackageLockTree({
33 physicalTree: physicalChild
34 })
35 return dependencies
36 }, {})
37 : undefined
38 }
39}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

5get lockfileName() {
6 return 'package-lock.json';
7}
5function generatePackageJson() {
6 const mainFile = path.resolve(__dirname, '../package.json');
7 const libTemplateFile = path.resolve(__dirname, '../src/ng5-slider/package.json.template');
8 const libFile = path.resolve(__dirname, '../src/ng5-slider/package.json');
9
10 const libTemplateConfig = JSON.parse(fs.readFileSync(libTemplateFile, { encoding: 'utf8' }));
11 const mainConfig = JSON.parse(fs.readFileSync(mainFile, { encoding: 'utf8' }));
12
13 let libConfig = {};
14
15 for (let key of libTemplateConfig.keysToCopyFromMainPackageJson) {
16 libConfig[key] = mainConfig[key];
17 }
18 libConfig.dependencies = {};
19 for (let dependency of libTemplateConfig.dependenciesToCopyFromMainPackageJson) {
20 libConfig.dependencies[dependency] = mainConfig.dependencies[dependency];
21 }
22 libConfig.peerDependencies = {};
23 for (let dependency of libTemplateConfig.dependenciesToCopyAsPeerDependenciesFromMainPackageJson) {
24 libConfig.peerDependencies[dependency] = mainConfig.dependencies[dependency];
25 }
26
27 libConfig = Object.assign({}, libConfig, libTemplateConfig, libConfig);
28 delete libConfig.keysToCopyFromMainPackageJson;
29 delete libConfig.dependenciesToCopyFromMainPackageJson;
30 delete libConfig.dependenciesToCopyAsPeerDependenciesFromMainPackageJson;
31
32 for (let dependency of libTemplateConfig.dependenciesToCopyFromMainPackageJson) {
33 libConfig.ngPackage.whitelistedNonPeerDependencies.push(dependency);
34 }
35
36 const prettyPrintedLibConfig = JSON.stringify(libConfig, null, 2);
37
38 fs.writeFileSync(libFile, prettyPrintedLibConfig, { encoding: 'utf8' });
39}
29async function createPackageJson() {
30 const packageBuffer = await readFile(path.resolve(root, './package.json'));
31 const packageJson = JSON.parse(packageBuffer.toString());
32
33 const minimalPackage = {
34 author: packageJson.author,
35 version: packageJson.version,
36 description: packageJson.description,
37 keywords: packageJson.keywords,
38 repository: packageJson.repository,
39 license: packageJson.license,
40 bugs: packageJson.bugs,
41 homepage: packageJson.homepage,
42 peerDependencies: packageJson.peerDependencies,
43 dependencies: packageJson.dependencies,
44 name: 'hocify',
45 main: packageJson.main,
46 };
47
48 await writeFile(
49 path.resolve(root, './build/package.json'),
50 JSON.stringify(minimalPackage, null, 2),
51 );
52}

Related snippets