Skip to content

Commit

Permalink
feat(js): add publish target and generate minimal publish script for …
Browse files Browse the repository at this point in the history
…publishable libraries
  • Loading branch information
nartc committed Apr 12, 2022
1 parent 945ee01 commit cf66ea4
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
57 changes: 57 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,5 +794,62 @@ describe('lib', () => {
expect(tree.exists('libs/my-lib/package.json')).toBeTruthy();
});
});

describe('--publishable', () => {
it('should generate the build target', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'myLib',
publishable: true,
importPath: '@proj/my-lib',
compiler: 'tsc',
});

const config = readProjectConfiguration(tree, 'my-lib');
expect(config.targets.build).toEqual({
executor: '@nrwl/js:tsc',
options: {
assets: ['libs/my-lib/*.md'],
main: 'libs/my-lib/src/index.ts',
outputPath: 'dist/libs/my-lib',
tsConfig: 'libs/my-lib/tsconfig.lib.json',
},
outputs: ['{options.outputPath}'],
});
});

it('should generate the publish target', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'myLib',
publishable: true,
importPath: '@proj/my-lib',
compiler: 'tsc',
});

const config = readProjectConfiguration(tree, 'my-lib');
expect(config.targets.publish).toEqual({
executor: '@nrwl/workspace:run-commands',
options: {
command:
'node tools/scripts/publish.mjs my-lib {args.ver} {args.tag}',
cwd: 'dist/libs/my-lib',
},
dependsOn: [{ projects: 'self', target: 'build' }],
});
});

it('should generate publish script', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'myLib',
publishable: true,
importPath: '@proj/my-lib',
compiler: 'tsc',
});

expect(tree.exists('tools/scripts/publish.mjs')).toBeTruthy();
});
});
});
});
19 changes: 17 additions & 2 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getRootTsConfigPathInTree,
} from '@nrwl/workspace/src/utilities/typescript';
import { join } from 'path';
import { addMinimalPublishScript } from '../../utils/minimal-publish-script';
import { LibraryGeneratorSchema } from '../../utils/schema';
import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { addSwcDependencies } from '../../utils/swc/add-swc-dependencies';
Expand Down Expand Up @@ -91,12 +92,13 @@ function addProject(
tags: options.parsedTags,
};

if (options.buildable && options.config != 'npm-scripts') {
if (options.buildable && options.config !== 'npm-scripts') {
const outputPath = `dist/${destinationDir}/${options.projectDirectory}`;
projectConfiguration.targets.build = {
executor: `@nrwl/js:${options.compiler}`,
outputs: ['{options.outputPath}'],
options: {
outputPath: `dist/${destinationDir}/${options.projectDirectory}`,
outputPath,
main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'),
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
assets: [`${options.projectRoot}/*.md`],
Expand All @@ -106,6 +108,19 @@ function addProject(
if (options.compiler === 'swc' && options.skipTypeCheck) {
projectConfiguration.targets.build.options.skipTypeCheck = true;
}

if (options.publishable) {
const publishScriptPath = addMinimalPublishScript(tree);

projectConfiguration.targets.publish = {
executor: '@nrwl/workspace:run-commands',
options: {
command: `node ${publishScriptPath} ${options.name} {args.ver} {args.tag}`,
cwd: outputPath,
},
dependsOn: [{ projects: 'self', target: 'build' }],
};
}
}

if (options.config === 'workspace') {
Expand Down
44 changes: 44 additions & 0 deletions packages/js/src/utils/minimal-publish-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Tree } from '@nrwl/devkit';

const publishScriptContent = `
/**
* This is a minimal script to publish your package to "npm".
* This is meant to be used as-is or customize as you see fit.
*
* This script is executed on "dist/path/to/library" as "cwd" by default.
*
* You might need to authenticate with NPM before running this script.
*/
import { execSync } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
import * as chalk from 'chalk';
// Executing publish script: node path/to/publish.mjs {name} --version {version} --tag {tag}
// Default "tag" to "next" so we won't publish the "latest" tag by accident.
const [, , name, version, tag = 'next'] = process.argv;
// A simple SemVer validation to validate the version
const validVersion = /^\\d+\\.\\d+\\.\\d(-\\w+\\.\\d+)?/;
if (!version || !validVersion.test(version)) {
console.error(chalk.red(\`Invalid version\`));
process.exit(1);
}
// Updating the version in "package.json" before publishing
const json = JSON.parse(readFileSync(\`package.json\`).toString());
json.version = version;
writeFileSync(\`package.json\`, JSON.stringify(json, null, 2));
// Execute "npm publish" to publish
execSync(\`npm publish --access public\`);
`;

export function addMinimalPublishScript(tree: Tree) {
const publishScriptPath = 'tools/scripts/publish.mjs';
if (!tree.exists(publishScriptPath)) {
tree.write(publishScriptPath, publishScriptContent);
}

return publishScriptPath;
}

0 comments on commit cf66ea4

Please sign in to comment.