Skip to content

Commit

Permalink
feat(import) Add --dest option to explicitly specify import destina…
Browse files Browse the repository at this point in the history
…tion (#1772)
  • Loading branch information
Rafe authored and evocateur committed Nov 23, 2018
1 parent cd34b48 commit 9d36654
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
7 changes: 7 additions & 0 deletions commands/import/README.md
Expand Up @@ -27,3 +27,10 @@ When importing repositories with merge commits with conflicts, the import comman
$ lerna import ~/Product --flatten
```

### `--dest`

When importing repositories, you can specify the destination directory by the directory listed in lerna.json.

```
$ lerna import ~/Product --dest=utilities
```
@@ -0,0 +1,7 @@
{
"version": "1.0.0",
"packages": [
"core/*",
"packages/*"
]
}
@@ -0,0 +1,3 @@
{
"name": "multi-packages"
}
29 changes: 29 additions & 0 deletions commands/import/__tests__/import-command.test.js
Expand Up @@ -270,4 +270,33 @@ describe("ImportCommand", () => {
expect(await pathExists(packageJson)).toBe(true);
});
});

describe("with multi-packages Lerna dir", () => {
it("creates a module in specified package directory", async () => {
const [testDir, externalDir] = await Promise.all([
initFixture("multi-packages"),
initFixture("external", "Init external commit"),
]);

const packageJson = path.join(testDir, "packages", path.basename(externalDir), "package.json");

await lernaImport(testDir)(externalDir, "--dest=packages");

expect(await lastCommitInDir(testDir)).toBe("Init external commit");
expect(await pathExists(packageJson)).toBe(true);
});

it("throws error when the package directory does not match with config", async () => {
const [testDir, externalDir] = await Promise.all([
initFixture("multi-packages"),
initFixture("external", "Init external commit"),
]);

try {
await lernaImport(testDir)(externalDir, "--dest=components");
} catch (err) {
expect(err.message).toBe("--dest does not match with the package directories: core,packages");
}
});
});
});
5 changes: 5 additions & 0 deletions commands/import/command.js
Expand Up @@ -16,6 +16,11 @@ exports.builder = yargs =>
describe: "Import each merge commit as a single change the merge introduced",
type: "boolean",
},
dest: {
group: "Command Options:",
describe: "Import destination directory for the external git repository",
type: "string",
},
y: {
group: "Command Options:",
describe: "Skip all confirmation prompts",
Expand Down
24 changes: 17 additions & 7 deletions commands/import/index.js
Expand Up @@ -60,7 +60,14 @@ class ImportCommand extends Command {
}

// Compute a target directory relative to the Lerna root
const targetDir = path.join(this.getTargetBase(), externalRepoBase);
const targetBase = this.getTargetBase();
if (this.getPackageDirectories().indexOf(targetBase) === -1) {
throw new ValidationError(
"EDESTDIR",
`--dest does not match with the package directories: ${this.getPackageDirectories()}`
);
}
const targetDir = path.join(targetBase, externalRepoBase);

// Compute a target directory relative to the Git root
const gitRepoRoot = this.getWorkspaceRoot();
Expand Down Expand Up @@ -105,13 +112,16 @@ class ImportCommand extends Command {
return PromptUtilities.confirm("Are you sure you want to import these commits onto the current branch?");
}

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

getTargetBase() {
return (
this.project.packageConfigs
.filter(p => path.basename(p) === "*")
.map(p => path.dirname(p))
.shift() || "packages"
);
if (this.options.dest) {
return this.options.dest;
}

return this.getPackageDirectories().shift() || "packages";
}

getCurrentSHA() {
Expand Down

0 comments on commit 9d36654

Please sign in to comment.