Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle additional yarn workspace definition formats #463

Merged
merged 2 commits into from Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/dependency-manager-adapters/npm.js
Expand Up @@ -194,10 +194,13 @@ module.exports = CoreObject.extend({
let restoreTasks = [
copy(path.join(this.cwd, this.packageJSONBackupFileName),
path.join(this.cwd, this.packageJSON)),
copy(path.join(this.cwd, this.nodeModulesBackupLocation),
path.join(this.cwd, this.nodeModules), { clobber: true }),
];

if (fs.existsSync(path.join(this.cwd, this.nodeModulesBackupLocation))) {
restoreTasks.push(copy(path.join(this.cwd, this.nodeModulesBackupLocation),
path.join(this.cwd, this.nodeModules), { clobber: true }));
}
Comment on lines +199 to +202
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yarn workspaces can hoist packages in such a way that a node_modules folder may never be created. This checks the existence of the directory before trying to restore it


if (fs.existsSync(path.join(this.cwd, this.yarnLockBackupFileName))) {
restoreTasks.push(copy(path.join(this.cwd, this.yarnLockBackupFileName),
path.join(this.cwd, this.yarnLock)));
Expand All @@ -222,9 +225,12 @@ module.exports = CoreObject.extend({

let backupTasks = [
copy(path.join(this.cwd, this.packageJSON),
path.join(this.cwd, this.packageJSONBackupFileName)),
copy(path.join(this.cwd, this.nodeModules),
path.join(this.cwd, this.nodeModulesBackupLocation), { clobber: true })];
path.join(this.cwd, this.packageJSONBackupFileName))];

if (fs.existsSync(path.join(this.cwd, this.nodeModules))) {
backupTasks.push(copy(path.join(this.cwd, this.nodeModules),
path.join(this.cwd, this.nodeModulesBackupLocation), { clobber: true }));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above, but when backing up packages


if (fs.existsSync(path.join(this.cwd, this.yarnLock))) {
backupTasks.push(copy(path.join(this.cwd, this.yarnLock),
Expand Down
6 changes: 5 additions & 1 deletion lib/dependency-manager-adapters/workspace.js
Expand Up @@ -28,7 +28,11 @@ module.exports = CoreObject.extend({
}

let packageJSON = JSON.parse(fs.readFileSync(this.packageJSON));
let workspaceGlobs = packageJSON.workspaces;
let workspaceGlobs = packageJSON.workspaces

if (workspaceGlobs && workspaceGlobs.packages) {
workspaceGlobs = workspaceGlobs.packages
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tweak this to be specific about what we expect? I'm thinking of something like:

let workspaceGlobs = Array.isArray(packageJSON.workspaces) ? packageJSON.workspaces : packageJSON.workspaces.packages;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Ended up with

if (Array.isArray(packageJSON.workspaces)) {
  workspaceGlobs = packageJSON.workspaces
}

if (packageJSON.workspaces && Array.isArray(packageJSON.workspaces.packages)) {
  workspaceGlobs = packageJSON.workspaces.packages
}

so it's specific about each case


if (!workspaceGlobs || !workspaceGlobs.length) {
throw new Error('you must define the `workspaces` property in package.json with at least one workspace to use workspaces with ember-try');
Expand Down
27 changes: 27 additions & 0 deletions test/dependency-manager-adapters/workspace-adapter-test.js
Expand Up @@ -44,6 +44,33 @@ describe('workspaceAdapter', () => {
});
});

it('with workspace packages', () => {
writeJSONFile('package.json', {
name: 'a-test-project-with-workspaces',
workspaces: {
packages: [
"packages/*"
],
nohoist: [
'@foo/example'
]
}
})

fs.ensureDirSync('packages/test/node_modules');

writeJSONFile('packages/test/package.json', { originalPackageJSON: true });
writeJSONFile('packages/test/node_modules/prove-it.json', { originalNodeModules: true });

return new WorkspaceAdapter({
cwd: tmpdir,
useYarnCommand: true,
}).setup().then(() => {
assertFileContainsJSON(path.join(tmpdir, 'packages/test/package.json.ember-try'), { originalPackageJSON: true });
assertFileContainsJSON(path.join(tmpdir, 'packages/test/.node_modules.ember-try/prove-it.json'), { originalNodeModules: true });
});
});

it('backs up the yarn.lock file, npm-shrinkwrap.json and package-lock.json if they exist', () => {
fs.ensureDirSync('packages/test/node_modules');

Expand Down