diff --git a/docs/generated/api-workspace/generators/convert-to-buildable.md b/docs/generated/api-workspace/generators/convert-to-buildable.md new file mode 100644 index 0000000000000..b550aa9c70981 --- /dev/null +++ b/docs/generated/api-workspace/generators/convert-to-buildable.md @@ -0,0 +1,60 @@ +--- +title: '@nrwl/workspace:convert-to-buildable generator' +description: 'Convert an Nx library to be buildable' +--- + +# @nrwl/workspace:convert-to-buildable + +Convert an Nx library to be buildable + +## Usage + +```bash +nx generate convert-to-buildable ... +``` + +By default, Nx will search for `convert-to-buildable` in the default collection provisioned in `workspace.json`. + +You can specify the collection explicitly as follows: + +```bash +nx g @nrwl/workspace:convert-to-buildable ... +``` + +Show what will be generated without writing to disk: + +```bash +nx g convert-to-buildable ... --dry-run +``` + +### Examples + +Convert the my-feature-lib project to be buildable: + +```bash +nx g @nrwl/workspace:convert-to-buildable --project my-feature-lib +``` + +## Options + +### project + +Type: `string` + +Name of project to convert to buildable lib. + +### skipFormat + +Default: `false` + +Type: `boolean` + +Skip formatting files. + +### type + +Type: `string` + +Possible values: `js`, `node`, `nest`, `next`, `react`, `detox`, `web` + +The type of library that this is. diff --git a/docs/map.json b/docs/map.json index 2a369c5a1703e..07422aafa35fb 100644 --- a/docs/map.json +++ b/docs/map.json @@ -512,6 +512,11 @@ "id": "convert-to-nx-project-generator", "file": "generated/api-workspace/generators/convert-to-nx-project" }, + { + "name": "convert-to-buildable generator", + "id": "convert-to-buildable-generator", + "file": "generated/api-workspace/generators/convert-to-buildable" + }, { "name": "run-commands executor", "id": "run-commands-executor", diff --git a/packages/workspace/generators.json b/packages/workspace/generators.json index 7ce1f216bd378..5115a41166eb6 100644 --- a/packages/workspace/generators.json +++ b/packages/workspace/generators.json @@ -72,6 +72,12 @@ "description": "Moves a project's configuration outside of workspace.json" }, + "convert-to-buildable": { + "factory": "./src/generators/convert-to-buildable/convert-to-buildable#convertToBuildableSchematic", + "schema": "./src/generators/convert-to-buildable/schema.json", + "description": "Convert an Nx library to be buildable" + }, + "npm-package": { "factory": "./src/generators/npm-package/npm-package#npmPackageSchematic", "schema": "./src/generators/npm-package/schema.json", @@ -150,6 +156,12 @@ "description": "Moves a project's configuration outside of workspace.json" }, + "convert-to-buildable": { + "factory": "./src/generators/convert-to-buildable/convert-to-buildable#convertToBuildable", + "schema": "./src/generators/convert-to-buildable/schema.json", + "description": "Convert an Nx library to be buildable" + }, + "npm-package": { "factory": "./src/generators/npm-package/npm-package#npmPackageGenerator", "schema": "./src/generators/npm-package/schema.json", diff --git a/packages/workspace/src/generators/convert-to-buildable/convert-to-buildable.ts b/packages/workspace/src/generators/convert-to-buildable/convert-to-buildable.ts new file mode 100644 index 0000000000000..d6e4ffe83e60c --- /dev/null +++ b/packages/workspace/src/generators/convert-to-buildable/convert-to-buildable.ts @@ -0,0 +1,115 @@ +import { + NxJsonConfiguration, + readJsonFile, + readProjectConfiguration, + formatFiles, + Tree, + writeJson, + convertNxGenerator, + updateProjectConfiguration, +} from '@nrwl/devkit'; + +import { Schema } from './schema'; +import { join } from 'path'; + +function getExecutor(type: Schema['type']): string { + switch (type) { + case 'detox': + case 'js': + case 'web': + return '@nrwl/js:tsc'; + case 'next': + case 'react': + return '@nrwl/web:rollup'; + case 'node': + case 'nest': + return '@nrwl/node:package'; + } +} + +export async function convertToBuildable(host: Tree, schema: Schema) { + const configuration = readProjectConfiguration(host, schema.project); + + if (configuration.projectType !== 'library') { + throw new Error(`${schema.project} is not a library.`); + } + + const nxJson = readJsonFile('nx.json'); + + if (configuration.targets['build'] != null) { + throw new Error(`${schema.project} is already buildable.`); + } + + /* + * Note on this: It seems new workspaces default to `packages` even though + * it is not set in nx.json whereas old workspaces may be using `libs` but + * it is also not set in the nx.json. + */ + const libDir = nxJson.workspaceLayout?.libsDir ?? 'packages'; + + const projectImport = `@${nxJson.npmScope}/${configuration.root.replace( + `${libDir}/`, + '' + )}`; + + // Write the package.json the builder will need + writeJson(host, join(configuration.root, 'package.json'), { + name: projectImport, + version: '0.0.1', + }); + + // Write the build target to the projects configuration + const buildTarget: any = { + executor: getExecutor(schema.type), + outputs: ['{options.outputPath}'], + options: { + outputPath: `dist/${configuration.root}`, + tsConfig: `${configuration.root}/tsconfig.lib.json`, + main: `${configuration.root}/src/index.ts`, + assets: [`${configuration.root}/*.md`], + }, + }; + + switch (schema.type) { + case 'nest': + case 'node': + buildTarget.options['packageJson'] = `${configuration.root}/package.json`; + break; + case 'next': + case 'react': + const { main, ...options } = buildTarget.options; + buildTarget.options = { + ...options, + project: `${configuration.root}/package.json`, + entryFile: `${configuration.root}/src/index.ts`, + external: ['react/jsx-runtime'], + rollupConfig: '@nrwl/react/plugins/bundle-rollup', + compiler: 'babel', + assets: [ + { + glob: `${configuration.root}/README.md`, + input: '.', + output: '.', + }, + ], + }; + break; + } + + updateProjectConfiguration(host, schema.project, { + ...configuration, + targets: { + ...configuration.targets, + build: buildTarget, + }, + }); + + if (!schema.skipFormat) { + await formatFiles(host); + } +} + +export default convertToBuildable; + +export const convertToBuildableSchematic = + convertNxGenerator(convertToBuildable); diff --git a/packages/workspace/src/generators/convert-to-buildable/schema.d.ts b/packages/workspace/src/generators/convert-to-buildable/schema.d.ts new file mode 100644 index 0000000000000..c24242aa36d29 --- /dev/null +++ b/packages/workspace/src/generators/convert-to-buildable/schema.d.ts @@ -0,0 +1,5 @@ +export interface Schema { + project: string; + type: 'js' | 'node' | 'nest' | 'next' | 'react' | 'detox' | 'web'; + skipFormat?: boolean; +} diff --git a/packages/workspace/src/generators/convert-to-buildable/schema.json b/packages/workspace/src/generators/convert-to-buildable/schema.json new file mode 100644 index 0000000000000..15868aab4498d --- /dev/null +++ b/packages/workspace/src/generators/convert-to-buildable/schema.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json-schema.org/schema", + "$id": "SchematicsConvertToBuildable", + "title": "Convert library to be a buildable lib", + "type": "object", + "cli": "nx", + "examples": [ + { + "command": "g @nrwl/workspace:convert-to-buildable --project my-feature-lib", + "description": "Convert the my-feature-lib project to be buildable" + } + ], + "properties": { + "project": { + "description": "Name of project to convert to buildable lib.", + "type": "string" + }, + "skipFormat": { + "description": "Skip formatting files.", + "type": "boolean", + "default": false + }, + "type": { + "description": "The type of library that this is.", + "type": "string", + "enum": ["js", "node", "nest", "next", "react", "detox", "web"] + } + } +}