Skip to content

Commit

Permalink
fix(import): Ensure compatibility with root-level package globs (#1875)
Browse files Browse the repository at this point in the history
Fixes #1872
  • Loading branch information
Brian Mock authored and evocateur committed Jan 18, 2019
1 parent bfa89bf commit 16ab98d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 1 deletion.
@@ -0,0 +1,4 @@
{
"version": "1.0.0",
"packages": ["mycompany-*"]
}
@@ -0,0 +1,3 @@
{
"name": "mycompany-root"
}
14 changes: 14 additions & 0 deletions commands/import/__tests__/import-command.test.js
Expand Up @@ -11,6 +11,7 @@ const pathExists = require("path-exists");
const PromptUtilities = require("@lerna/prompt");

// helpers
const initNamedFixture = require("@lerna-test/init-named-fixture")(__dirname);
const initFixture = require("@lerna-test/init-fixture")(__dirname);
const gitAdd = require("@lerna-test/git-add");
const gitCommit = require("@lerna-test/git-commit");
Expand Down Expand Up @@ -78,6 +79,19 @@ describe("ImportCommand", () => {
expect(await pathExists(newFilePath)).toBe(true);
});

it("imports a repo into the root directory when packages are located there", async () => {
const [testDir, externalDir] = await Promise.all([
initFixture("root-packages"),
initNamedFixture("myapp-foo", "external", "myapp-foo init commit"),
]);

await lernaImport(testDir)(externalDir);

expect(await lastCommitInDir(testDir)).toBe("myapp-foo init commit");
expect(await pathExists(path.join(testDir, "myapp-foo/old-file"))).toBe(true);
expect(await pathExists(path.join(testDir, "myapp-foo/package.json"))).toBe(true);
});

it("supports moved files within the external repo", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const newFilePath = path.join(testDir, "packages", path.basename(externalDir), "new-file");
Expand Down
2 changes: 1 addition & 1 deletion commands/import/index.js
Expand Up @@ -114,7 +114,7 @@ class ImportCommand extends Command {
}

getPackageDirectories() {
return this.project.packageConfigs.filter(p => path.basename(p) === "*").map(p => path.dirname(p));
return this.project.packageConfigs.filter(p => p.endsWith("*")).map(p => path.dirname(p));
}

getTargetBase() {
Expand Down
30 changes: 30 additions & 0 deletions helpers/init-named-fixture/index.js
@@ -0,0 +1,30 @@
"use strict";

const path = require("path");
const fs = require("fs-extra");
const tempy = require("tempy");
const copyFixture = require("@lerna-test/copy-fixture");
const gitAdd = require("@lerna-test/git-add");
const gitCommit = require("@lerna-test/git-commit");
const gitInit = require("@lerna-test/git-init");

module.exports = initNamedFixture;

function initNamedFixture(startDir) {
return (dirName, fixtureName, commitMessage = "Init commit") => {
const cwd = path.join(tempy.directory(), dirName);
let chain = Promise.resolve();

chain = chain.then(() => fs.ensureDir(cwd));
chain = chain.then(() => process.chdir(cwd));
chain = chain.then(() => copyFixture(cwd, fixtureName, startDir));
chain = chain.then(() => gitInit(cwd, "."));

if (commitMessage) {
chain = chain.then(() => gitAdd(cwd, "-A"));
chain = chain.then(() => gitCommit(cwd, commitMessage));
}

return chain.then(() => cwd);
};
}
16 changes: 16 additions & 0 deletions helpers/init-named-fixture/package.json
@@ -0,0 +1,16 @@
{
"name": "@lerna-test/init-named-fixture",
"version": "0.0.0-test-only",
"description": "A local test helper",
"main": "index.js",
"private": true,
"license": "MIT",
"dependencies": {
"@lerna-test/copy-fixture": "file:../copy-fixture",
"@lerna-test/git-add": "file:../git-add",
"@lerna-test/git-commit": "file:../git-commit",
"@lerna-test/git-init": "file:../git-init",
"tempy": "^0.2.1",
"fs-extra": "^7.0.0"
}
}
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"@lerna-test/git-status": "file:helpers/git-status",
"@lerna-test/git-tag": "file:helpers/git-tag",
"@lerna-test/init-fixture": "file:helpers/init-fixture",
"@lerna-test/init-named-fixture": "file:helpers/init-named-fixture",
"@lerna-test/load-manifests": "file:helpers/load-manifests",
"@lerna-test/logging-output": "file:helpers/logging-output",
"@lerna-test/multi-line-trim-right": "file:helpers/multi-line-trim-right",
Expand Down

0 comments on commit 16ab98d

Please sign in to comment.