-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): standalone executor for generating package.json
* generates a package.json based on the project's dependencies in node_modules * moves getExtraDependencies helper from esbuild package to js package
- Loading branch information
Showing
15 changed files
with
512 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
docs/generated/packages/js/executors/generate-package-json.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"name": "generate-package-json", | ||
"implementation": "/packages/js/src/executors/generate-package-json/generate-package-json.impl.ts", | ||
"schema": { | ||
"$schema": "http://json-schema.org/schema", | ||
"outputCapture": "direct-nodejs", | ||
"version": 2, | ||
"cli": "nx", | ||
"title": "GeneratePackageJson builder", | ||
"description": "Generates a package.json file", | ||
"type": "object", | ||
"properties": { | ||
"main": { | ||
"type": "string", | ||
"description": "The path to the entry file, relative to project.", | ||
"alias": "entryFile", | ||
"x-completion-type": "file", | ||
"x-completion-glob": "**/*@(.js|.ts)" | ||
}, | ||
"outputPath": { | ||
"type": "string", | ||
"description": "The output path of the generated files.", | ||
"x-completion-type": "directory", | ||
"x-priority": "important" | ||
}, | ||
"tsConfig": { | ||
"type": "string", | ||
"description": "The path to tsconfig file.", | ||
"x-completion-type": "file", | ||
"x-completion-glob": "tsconfig.*.json" | ||
}, | ||
"buildableProjectDepsInPackageJsonType": { | ||
"type": "string", | ||
"description": "The type of dependency to use for buildable project dependencies in the generated `package.json`.", | ||
"enum": ["dependencies", "devDependencies"], | ||
"default": "dependencies" | ||
}, | ||
"excludeLibsInPackageJson": { | ||
"type": "boolean", | ||
"description": "Exclude libraries in the `package.json` file. This is useful if you are using a `package.json` file in the project's directory.", | ||
"default": true | ||
}, | ||
"generateLockfile": { | ||
"type": "boolean", | ||
"description": "Generate a lock file for the generated `package.json`.", | ||
"default": true | ||
}, | ||
"format": { | ||
"type": "string", | ||
"description": "List of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise).", | ||
"alias": "f", | ||
"enum": ["esm", "cjs"], | ||
"default": "esm" | ||
} | ||
}, | ||
"definitions": {}, | ||
"required": ["main", "outputPath", "tsConfig"], | ||
"examplesFile": "---\ntitle: Examples for the generate package json executor\ndescription: This page contains examples for the js @nrwl/js:generate-package-json executor.\n---\n\n`project.json`:\n\n```json\n{\n \"name\": \"api\",\n //...\n \"targets\": {\n \"build\": {\n \"dependsOn\": [\n { \"projects\": \"self\", \"target\": \"build\", \"params\": \"forward\" }\n ],\n \"executor\": \"@nrwl/js:generate-package-json\",\n \"options\": {\n \"outputPath\": \"dist/apps/api\",\n \"main\": \"apps/api/src/main.ts\",\n \"tsConfig\": \"apps/api/tsconfig.app.json\"\n }\n },\n \"build-api\": {\n \"executor\": \"@nrwl/vite:build\",\n //...\n \"configurations\": {\n \"production\": {\n \"mode\": \"production\"\n }\n }\n },\n \"serve\": {\n \"executor\": \"@nrwl/vite:dev-server\",\n //...\n \"configurations\": {\n \"production\": {\n \"buildTarget\": \"api:build:production\"\n }\n }\n }\n }\n}\n```\n\n```bash\nnx build api --prod\nnx build api --dev\n```\n", | ||
"presets": [] | ||
}, | ||
"description": "Generates a `package.json` and pruned lock file (if generateLockfile not disabled) with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated.", | ||
"aliases": [], | ||
"hidden": false, | ||
"path": "/packages/js/src/executors/generate-package-json/schema.json", | ||
"type": "executor" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: Examples for the generate package json executor | ||
description: This page contains examples for the js @nrwl/js:generate-package-json executor. | ||
--- | ||
|
||
`project.json`: | ||
|
||
```json | ||
{ | ||
"name": "api", | ||
//... | ||
"targets": { | ||
"build": { | ||
"dependsOn": [ | ||
{ "projects": "self", "target": "build", "params": "forward" } | ||
], | ||
"executor": "@nrwl/js:generate-package-json", | ||
"options": { | ||
"outputPath": "dist/apps/api", | ||
"main": "apps/api/src/main.ts", | ||
"tsConfig": "apps/api/tsconfig.app.json" | ||
} | ||
}, | ||
"build-api": { | ||
"executor": "@nrwl/vite:build", | ||
//... | ||
"configurations": { | ||
"production": { | ||
"mode": "production" | ||
} | ||
} | ||
}, | ||
"serve": { | ||
"executor": "@nrwl/vite:dev-server", | ||
//... | ||
"configurations": { | ||
"production": { | ||
"buildTarget": "api:build:production" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```bash | ||
nx build api --prod | ||
nx build api --dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
packages/js/src/executors/generate-package-json/generate-package-json.impl.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import 'nx/src/utils/testing/mock-fs'; | ||
|
||
import { GeneratePackageJsonExecutorOptions } from './schema'; | ||
import executor from './generate-package-json.impl'; | ||
import { ExecutorContext } from '@nrwl/devkit'; | ||
import { vol } from 'memfs'; | ||
import { readFileSync, statSync } from 'fs'; | ||
import { readdirSync } from 'fs-extra'; | ||
import { join } from 'path'; | ||
|
||
const getAllFiles = (dirPath: string, arrayOfFiles = []) => { | ||
const files = readdirSync(dirPath); | ||
|
||
files.forEach((file) => { | ||
const filepath = join(dirPath, file); | ||
if (statSync(filepath).isDirectory()) { | ||
arrayOfFiles = getAllFiles(filepath, arrayOfFiles); | ||
} else { | ||
arrayOfFiles.push(filepath); | ||
} | ||
}); | ||
|
||
return arrayOfFiles; | ||
}; | ||
|
||
describe('GeneratePackageJson Executor', () => { | ||
const projectName = 'parent'; | ||
const projectPath = `apps/${projectName}`; | ||
const outputPath = `dist/apps/${projectName}`; | ||
|
||
const mockWorkspaceRoot = process.cwd(); | ||
|
||
const mockBuildOptions: GeneratePackageJsonExecutorOptions = { | ||
main: `${projectPath}/index.js`, | ||
outputPath, | ||
tsConfig: `${projectPath}/tsconfig.app.json`, | ||
}; | ||
|
||
const mockContext: ExecutorContext = { | ||
cwd: '', | ||
isVerbose: false, | ||
root: mockWorkspaceRoot, | ||
projectName, | ||
target: { | ||
options: mockBuildOptions, | ||
}, | ||
targetName: 'build', | ||
projectGraph: { | ||
nodes: { | ||
parent: { | ||
type: 'app', | ||
name: 'parent', | ||
data: { | ||
files: [], | ||
root: projectPath, | ||
targets: { | ||
build: { | ||
inputs: [], | ||
options: mockBuildOptions, | ||
}, | ||
}, | ||
}, | ||
}, | ||
child1: { | ||
type: 'lib', | ||
name: 'child1', | ||
data: {}, | ||
}, | ||
child2: { | ||
type: 'lib', | ||
name: 'child2', | ||
data: {}, | ||
}, | ||
} as any, | ||
externalNodes: { | ||
'npm:react': { | ||
type: 'npm', | ||
name: 'npm:react', | ||
data: { packageName: 'react', version: '18.0.0' }, | ||
}, | ||
'npm:axios': { | ||
type: 'npm', | ||
name: 'npm:axios', | ||
data: { packageName: 'axios', version: '1.0.0' }, | ||
}, | ||
'npm:dayjs': { | ||
type: 'npm', | ||
name: 'npm:dayjs', | ||
data: { packageName: 'dayjs', version: '1.11.0' }, | ||
}, | ||
}, | ||
dependencies: { | ||
parent: [ | ||
{ source: 'parent', target: 'child1', type: 'static' }, | ||
{ source: 'parent', target: 'npm:react', type: 'static' }, | ||
], | ||
child1: [ | ||
{ source: 'child1', target: 'child2', type: 'static' }, | ||
{ source: 'child1', target: 'npm:axios', type: 'static' }, | ||
], | ||
child2: [{ source: 'child2', target: 'npm:dayjs', type: 'static' }], | ||
}, | ||
}, | ||
}; | ||
|
||
beforeEach(async () => { | ||
vol.reset(); | ||
const fileSys = { | ||
[`${projectPath}/package.json`]: JSON.stringify({ | ||
name: '@company/parent', | ||
author: 'John Doe', | ||
something: 'else', | ||
}), | ||
'package.json': JSON.stringify({ | ||
name: 'package.name.does.not.matter', | ||
}), | ||
'package-lock.json': JSON.stringify({ | ||
name: 'package-lock.name.does.not.matter', | ||
version: 'package-lock.version.does.not.matter', | ||
lockfileVersion: 3, | ||
packages: { | ||
'': {}, | ||
'node_modules/axios': { | ||
version: '1.0.0', | ||
}, | ||
'node_modules/dayjs': { | ||
version: '1.11.0', | ||
}, | ||
'node_modules/react': { | ||
version: '18.0.0', | ||
}, | ||
}, | ||
}), | ||
}; | ||
vol.fromJSON(fileSys, mockWorkspaceRoot); | ||
}); | ||
|
||
it('generates a package.json and package-lock.json with child dependencies', async () => { | ||
const output = await executor(mockBuildOptions, mockContext); | ||
|
||
const expectedPackageLockJson = { | ||
name: '@company/parent', | ||
lockfileVersion: 3, | ||
version: '0.0.1', | ||
packages: { | ||
'': { | ||
name: '@company/parent', | ||
dependencies: { | ||
axios: '1.0.0', | ||
dayjs: '1.11.0', | ||
react: '18.0.0', | ||
}, | ||
}, | ||
'node_modules/axios': { | ||
version: '1.0.0', | ||
}, | ||
'node_modules/dayjs': { | ||
version: '1.11.0', | ||
}, | ||
'node_modules/react': { | ||
version: '18.0.0', | ||
}, | ||
}, | ||
}; | ||
|
||
const expectedPackageJson = { | ||
name: '@company/parent', | ||
author: 'John Doe', | ||
something: 'else', | ||
dependencies: { | ||
axios: '1.0.0', | ||
dayjs: '1.11.0', | ||
react: '18.0.0', | ||
}, | ||
module: './index.js', | ||
type: 'module', | ||
main: './index.js', | ||
}; | ||
|
||
expect( | ||
JSON.parse(readFileSync(`${outputPath}/package-lock.json`, 'utf-8')) | ||
).toEqual(expectedPackageLockJson); | ||
|
||
expect( | ||
JSON.parse(readFileSync(`${outputPath}/package.json`, 'utf-8')) | ||
).toEqual(expectedPackageJson); | ||
|
||
expect(output.success).toBe(true); | ||
|
||
expect(getAllFiles('')).toEqual([ | ||
`${projectPath}/package.json`, | ||
`${outputPath}/package-lock.json`, | ||
`${outputPath}/package.json`, | ||
'package-lock.json', | ||
'package.json', | ||
'tmp/apps/parent/tsconfig.generated.json', | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.