Skip to content

Commit

Permalink
chore: upgrade to lerna 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Aug 21, 2018
1 parent 47e9369 commit 12c24a8
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bin/build-docs-site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rm -rf sandbox/loopback.io/
git clone --depth 1 https://github.com/strongloop/loopback.io.git sandbox/loopback.io

# Bootstrap the `loopback.io` package
lerna bootstrap --scope loopback.io-workflow-scripts
lerna bootstrap --no-ci --scope loopback.io-workflow-scripts

pushd $REPO_ROOT/sandbox/loopback.io/ >/dev/null

Expand Down
31 changes: 31 additions & 0 deletions bin/config-lerna-scopes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/**
* This is an internal script to list lerna packages for loopback-next.
* See https://github.com/marionebl/commitlint/pull/406
*/
'use strict';

const getPackages = require('@lerna/project').getPackages;
module.exports = {
rules: {
// https://github.com/marionebl/commitlint/blob/master/docs/reference-rules.md
'scope-enum': async ctx => [2, 'always', await listPackages(ctx)],
},
};

async function listPackages(context) {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();
// List all lerna packages
const packages = await getPackages(cwd);

// Get a list of names. Npm scopes will be removed, for example,
// @loopback/core -> core
return packages
.map(pkg => pkg.name)
.map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name));
}
14 changes: 5 additions & 9 deletions bin/run-lerna.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@
'use strict';

const path = require('path');
const lerna = require('lerna');
const Project = require('@lerna/project');
const build = require('../packages/build');

function run(argv, options) {
const ls = new lerna.LsCommand(
null,
{json: true, loglevel: 'silent'},
path.join(__dirname, '..'),
);
const rootPath = ls.repository.rootPath;
async function run(argv, options) {
const project = new Project(path.join(__dirname, '..'));
const rootPath = project.rootPath;

process.env.LERNA_ROOT_PATH = rootPath;
let args = argv.slice(2);

return build.runCLI('lerna/bin/lerna', args, options);
return build.runCLI('lerna/cli', args, options);
}

module.exports = run;
Expand Down
2 changes: 1 addition & 1 deletion bin/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
# Running Code Linter -- Requires @loopback/build so it's bootstrapped
if [ $TASK = "code-lint" ]; then
echo "TASK => LINTING CODE"
lerna bootstrap --scope @loopback/build
lerna bootstrap --no-ci --scope @loopback/build
npm run lint

# Commit Message Linter
Expand Down
96 changes: 58 additions & 38 deletions bin/update-template-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,71 @@

const path = require('path');
const fs = require('fs');
const lerna = require('lerna');
const ls = new lerna.LsCommand(null, {json: true, loglevel: 'silent'});

// We don't have to run the command as the preparations will collect packages
ls.configureLogging();
ls.runValidations();
ls.runPreparations();

const pkgs = ls.filteredPackages.filter(pkg => !pkg.isPrivate()).map(pkg => ({
name: pkg.name,
version: pkg.version,
}));

const lbModules = {};
for (const p of pkgs) {
lbModules[p.name] = '^' + p.version;

const Command = require('@lerna/command');

/**
* A dummy command to get filtered packages
*/
class NopCommand extends Command {
get requiresGit() {
return false;
}

initialize() {
// No-op
}

execute() {
// No-op
}
}

const rootPath = ls.repository.rootPath;
async function updateTemplateDeps() {
const cmd = new NopCommand({_: [], loglevel: 'silent'});

// Load dependencies from `packages/build/package.json`
const buildDeps = require(path.join(rootPath, 'packages/build/package.json'))
.dependencies;
await cmd; // Execute the command

// Load dependencies from `packages/cli/package.json`
const cliPackageJson = path.join(rootPath, 'packages/cli/package.json');
const pkgs = cmd.filteredPackages.filter(pkg => !pkg.private).map(pkg => ({
name: pkg.name,
version: pkg.version,
}));

// Loading existing dependencies from `packages/cli/package.json`
const cliPkg = require(cliPackageJson);
cliPkg.config = cliPkg.config || {};
const currentDeps = cliPkg.config.templateDependencies || {};
const lbModules = {};
for (const p of pkgs) {
lbModules[p.name] = '^' + p.version;
}

// Merge all entries
const deps = Object.assign({}, currentDeps, buildDeps, lbModules);
const rootPath = cmd.project.rootPath;

cliPkg.config.templateDependencies = deps;
// Load dependencies from `packages/build/package.json`
const buildDeps = require(path.join(rootPath, 'packages/build/package.json'))
.dependencies;

// Convert to JSON
const json = JSON.stringify(cliPkg, null, 2);
// Load dependencies from `packages/cli/package.json`
const cliPackageJson = path.join(rootPath, 'packages/cli/package.json');

if (process.argv[2] === '-f') {
// Using `-f` to overwrite packages/cli/lib/dependencies.json
fs.writeFileSync(cliPackageJson, json + '\n', {encoding: 'utf-8'});
console.log('%s has been updated.', cliPackageJson);
} else {
// Otherwise write to console
console.log(json);
// Loading existing dependencies from `packages/cli/package.json`
const cliPkg = require(cliPackageJson);
cliPkg.config = cliPkg.config || {};
const currentDeps = cliPkg.config.templateDependencies || {};

// Merge all entries
const deps = Object.assign({}, currentDeps, buildDeps, lbModules);

cliPkg.config.templateDependencies = deps;

// Convert to JSON
const json = JSON.stringify(cliPkg, null, 2);

if (process.argv[2] === '-f') {
// Using `-f` to overwrite packages/cli/lib/dependencies.json
fs.writeFileSync(cliPackageJson, json + '\n', {encoding: 'utf-8'});
console.log('%s has been updated.', cliPackageJson);
} else {
// Otherwise write to console
console.log(json);
}
}

if (require.main === module) updateTemplateDeps();
4 changes: 3 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
module.exports = {
extends: [
'@commitlint/config-conventional',
'@commitlint/config-lerna-scopes',
// https://github.com/marionebl/commitlint/pull/406
// '@commitlint/config-lerna-scopes',
'./bin/config-lerna-scopes',
],
rules: {
'header-max-length': [2, 'always', 100],
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lerna": "2.9.1",
"lerna": "3.1.2",
"packages": [
"benchmark",
"docs",
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
"devDependencies": {
"@commitlint/cli": "^7.0.0",
"@commitlint/config-conventional": "^7.0.0",
"@commitlint/config-lerna-scopes": "^7.0.0",
"@commitlint/travis-cli": "^7.0.0",
"@types/mocha": "^5.0.0",
"coveralls": "^3.0.0",
"cz-conventional-changelog": "^2.1.0",
"husky": "^0.14.3",
"lerna": "^2.9.1"
"lerna": "^3.1.2"
},
"scripts": {
"postinstall": "lerna bootstrap",
"postinstall": "lerna bootstrap --no-ci",
"prerelease": "npm run build:full && npm run mocha && npm run lint",
"release": "lerna publish",
"update-template-deps": "node bin/update-template-deps -f",
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/test/acceptance/app-run.acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const path = require('path');
const assert = require('yeoman-assert');
const helpers = require('yeoman-test');
const lerna = require('lerna');
const bootstrapCommandFactory = require('@lerna/bootstrap');
const build = require('@loopback/build');

describe('app-generator (SLOW)', function() {
Expand Down Expand Up @@ -68,10 +68,12 @@ describe('app-generator (SLOW)', function() {
});
});

function lernaBootstrap(scope) {
const cmd = new lerna.BootstrapCommand('', {
async function lernaBootstrap(scope) {
const cmd = bootstrapCommandFactory({
_: [],
ci: false,
scope: scope,
loglevel: 'silent',
});
return cmd.run();
await cmd;
}

0 comments on commit 12c24a8

Please sign in to comment.