Skip to content

Commit

Permalink
feat(node): support 'npm' as a package manager (projen#157)
Browse files Browse the repository at this point in the history
Adds support for specifying `packageManager` for node projects (and derivatives). The default is still `yarn` but `npm` is also supported.

Closes projen#156
  • Loading branch information
RafalWilinski authored Oct 6, 2020
1 parent d216d80 commit f2a03d3
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: |-
git config user.name "Auto-bump"
git config user.email "github-actions@github.com"
- run: yarn build
- run: yarn run build
- name: Anti-tamper check
run: git diff --exit-code
container:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ jobs:
run: |-
git config user.name "Auto-bump"
git config user.email "github-actions@github.com"
- run: yarn bump
- run: yarn build
- run: yarn run bump
- run: yarn run build
- name: Anti-tamper check
run: git diff --exit-code
- run: git push --follow-tags origin $GITHUB_REF
Expand Down
2 changes: 1 addition & 1 deletion .versionrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
],
"commitAll": true,
"scripts": {
"postbump": "yarn projen && git add ."
"postbump": "yarn run projen && git add ."
}
}
33 changes: 33 additions & 0 deletions API.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"start": "npx projen start -i",
"projen": "node .projenrc.js",
"no-changes": "(git log --oneline -1 | grep -q \"chore(release):\") && echo \"No changes to release.\"",
"bump": "yarn --silent no-changes || standard-version",
"release": "yarn --silent no-changes || (yarn bump && git push --follow-tags origin master)",
"bump": "yarn run --silent no-changes || standard-version",
"release": "yarn run --silent no-changes || (yarn run bump && git push --follow-tags origin master)",
"projen:upgrade": "yarn upgrade -L projen && CI=\"\" yarn projen",
"bootstrap": "yarn install && yarn compile && yarn projen",
"compile": "jsii --silence-warnings=reserved-word --no-fix-peer-dependencies && jsii-docgen && yarn projen",
"watch": "jsii -w --silence-warnings=reserved-word --no-fix-peer-dependencies",
"build": "yarn compile && yarn test && yarn run package",
"build": "yarn run compile && yarn run test && yarn run package",
"package": "jsii-pacmak",
"test": "jest --passWithNoTests --updateSnapshot && yarn eslint",
"test": "jest --passWithNoTests --updateSnapshot && yarn run eslint",
"test:watch": "jest --watch",
"test:update": "jest --updateSnapshot",
"eslint": "eslint --ext .ts --fix src test",
Expand Down
2 changes: 1 addition & 1 deletion src/awscdk-app-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class AwsCdkTypeScriptApp extends TypeScriptAppProject {

this.addScript('compile', 'true');
this.removeScript('watch'); // because we use ts-node
this.addBuildCommand('yarn synth');
this.addBuildCommand(`${this.runScriptCommand} synth`);

this.start?.addEntry('synth', {
desc: 'Synthesizes your cdk app into cdk.out (part of "yarn build")',
Expand Down
2 changes: 1 addition & 1 deletion src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Eslint extends Component {
desc: 'Runs eslint against the codebase',
category: StartEntryCategory.TEST,
});
project.addTestCommand('yarn eslint');
project.addTestCommand(`${project.runScriptCommand} eslint`);

// exclude some files
project.npmignore?.exclude('/.eslintrc.json');
Expand Down
2 changes: 1 addition & 1 deletion src/jsii-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class JsiiProject extends TypeScriptProject {

const compat = options.compat ?? false;
if (compat) {
this.addCompileCommand('yarn compat');
this.addCompileCommand(`${this.runScriptCommand} compat`);
} else {
this.addTip('Set "compat" to "true" to enable automatic API breaking-change validation');
}
Expand Down
78 changes: 64 additions & 14 deletions src/node-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ import { Version } from './version';

const PROJEN_SCRIPT = 'projen';

/**
* The node package manager to use.
*/
export enum NodePackageManager {
/**
* Use `yarn` as the package manager.
*/
YARN,

/**
* Use `npm` as the package manager.
*/
NPM
}

export interface NodeProjectCommonOptions {
readonly bundledDependencies?: string[];
readonly dependencies?: Record<string, Semver>;
Expand Down Expand Up @@ -156,6 +171,13 @@ export interface NodeProjectCommonOptions {
*/
readonly npmRegistry?: string;

/**
* The Node Package Manager used to execute scripts
*
* @default packageManager.YARN
*/
readonly packageManager?: NodePackageManager;

/**
* License copyright owner.
*
Expand Down Expand Up @@ -447,6 +469,16 @@ export class NodeProject extends Project {

protected readonly npmRegistry: string;

/**
* The package manager to use.
*/
protected readonly packageManager: NodePackageManager;

/**
* The command to use to run scripts (e.g. `yarn run` or `npm run` depends on the package mabnager).
*/
public readonly runScriptCommand: string;

constructor(options: NodeProjectOptions) {
super();

Expand Down Expand Up @@ -474,6 +506,16 @@ export class NodeProject extends Project {

this.npmRegistry = options.npmRegistry ?? 'registry.npmjs.org';

this.packageManager = options.packageManager ?? NodePackageManager.YARN;

this.runScriptCommand = (() => {
switch (this.packageManager) {
case NodePackageManager.NPM: return 'npm run';
case NodePackageManager.YARN: return 'yarn run';
default: throw new Error(`unexpected package manager ${this.packageManager}`);
}
})();

this.scripts = {};

const renderScripts = () => {
Expand Down Expand Up @@ -1004,8 +1046,6 @@ export class NodeProject extends Project {
public postSynthesize(outdir: string) {
super.postSynthesize(outdir);

const install = ['yarn install'];

// now we run `yarn install`, but before we do that, remove the
// `node_modules/projen` symlink so that yarn won't hate us.
const projenModule = path.resolve('node_modules', 'projen');
Expand All @@ -1015,20 +1055,30 @@ export class NodeProject extends Project {
}
} catch (e) { }

// add --check-files to ensure all modules exist (especiall projen which was just removed).
install.push('--check-files');

// if we are running in a CI environment, fix versions through the lockfile.
if (process.env.CI) {
logging.info('Running yarn with --frozen-lockfile since "CI" is defined.');
install.push('--frozen-lockfile');
}

exec(install.join(' '), { cwd: outdir });
exec(this.installDepsCommand, { cwd: outdir });

this.resolveDependencies(outdir);
}

private get installDepsCommand() {
switch (this.packageManager) {
case NodePackageManager.YARN:
return [
'yarn install',
'--check-files', // ensure all modules exist (especially projen which was just removed).
...process.env.CI ? ['--frozen-lockfile'] : [],
].join(' ');

case NodePackageManager.NPM:
return process.env.CI
? 'npm ci'
: 'npm install';

default:
throw new Error(`unexpected package manager ${this.packageManager}`);
}
}

private loadDependencies(outdir: string) {
const root = path.join(outdir, 'package.json');

Expand Down Expand Up @@ -1228,10 +1278,10 @@ export class NodeBuildWorkflow extends GithubWorkflow {
},

// if there are changes, creates a bump commit
...options.bump ? [{ run: 'yarn bump' }] : [],
...options.bump ? [{ run: `${project.runScriptCommand} bump` }] : [],

// build (compile + test)
{ run: 'yarn build' },
{ run: `${project.runScriptCommand} build` },

// anti-tamper check (fails if there were changes to committed files)
// this will identify any non-commited files generated during build (e.g. test snapshots)
Expand Down
2 changes: 1 addition & 1 deletion src/projen-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ProjenUpgrade {
...project.workflowBootstrapSteps,

// upgrade
{ run: `yarn ${script}` },
{ run: `${project.runScriptCommand} ${script}` },

// submit a PR
{
Expand Down
9 changes: 5 additions & 4 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ export class TypeScriptProject extends NodeProject {

// by default, we first run tests (jest compiles the typescript in the background) and only then we compile.
const compileBeforeTest = options.compileBeforeTest ?? false;

if (compileBeforeTest) {
this.addBuildCommand('yarn compile', 'yarn test');
this.addBuildCommand(`${this.runScriptCommand} compile`, `${this.runScriptCommand} test`);
} else {
this.addBuildCommand('yarn test', 'yarn compile');
this.addBuildCommand(`${this.runScriptCommand} test`, `${this.runScriptCommand} compile`);
}
this.start?.addEntry('build', {
desc: 'Full release build (test+compile)',
Expand All @@ -156,11 +157,11 @@ export class TypeScriptProject extends NodeProject {
this.addScript('package',
'rm -fr dist',
'mkdir -p dist/js',
'yarn pack',
`${this.runScriptCommand} pack`,
'mv *.tgz dist/js/',
);

this.addBuildCommand('yarn run package');
this.addBuildCommand(`${this.runScriptCommand} package`);

this.start?.addEntry('package', {
desc: 'Create an npm tarball',
Expand Down
6 changes: 3 additions & 3 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export class Version extends Component {
super(project);

project.addScript('no-changes', '(git log --oneline -1 | grep -q "chore(release):") && echo "No changes to release."');
project.addScript('bump', 'yarn --silent no-changes || standard-version');
project.addScript('release', 'yarn --silent no-changes || (yarn bump && git push --follow-tags origin master)');
project.addScript('bump', `${project.runScriptCommand} --silent no-changes || standard-version`);
project.addScript('release', `${project.runScriptCommand} --silent no-changes || (${project.runScriptCommand} bump && git push --follow-tags origin master)`);

project.start?.addEntry('bump', {
desc: 'Commits a bump to the package version based on conventional commits',
Expand All @@ -39,7 +39,7 @@ export class Version extends Component {
commitAll: true,
scripts: {
// run projen after release to update package.json
postbump: 'yarn projen && git add .',
postbump: `${project.runScriptCommand} projen && git add .`,
},
},
});
Expand Down
Loading

0 comments on commit f2a03d3

Please sign in to comment.