Skip to content

Commit

Permalink
chore: refactor to single ensureConfig function to reduce duplicate l…
Browse files Browse the repository at this point in the history
…ogic
  • Loading branch information
fahslaj committed Jul 22, 2022
1 parent aaa8ab4 commit c00768e
Showing 1 changed file with 46 additions and 51 deletions.
97 changes: 46 additions & 51 deletions commands/init/index.js
Expand Up @@ -42,8 +42,7 @@ class InitCommand extends Command {
execute() {
let chain = Promise.resolve();

chain = chain.then(() => this.ensurePackageJSON());
chain = chain.then(() => this.ensureLernaConfig());
chain = chain.then(() => this.ensureConfig());
chain = chain.then(() => this.ensurePackagesDir());

return chain.then(() => {
Expand All @@ -52,14 +51,16 @@ class InitCommand extends Command {
});
}

get hasExistingLernaConfig() {
return !!this.project.version;
}
ensureConfig() {
const hasExistingLernaConfig = !!this.project.version;
const hasExistingPackageJson = !!this.project.manifest;

const useNx = !hasExistingLernaConfig || this.project.config.useNx === true;
const useWorkspaces = !hasExistingLernaConfig || this.project.config.useWorkspaces === true;

ensurePackageJSON() {
let chain = Promise.resolve();

if (!this.project.manifest) {
if (!hasExistingPackageJson) {
this.logger.info("", "Creating package.json");

// initialize with default indentation so write-pkg doesn't screw it up with tabs
Expand All @@ -69,7 +70,7 @@ class InitCommand extends Command {
private: true,
};

if (!this.hasExistingLernaConfig) {
if (useWorkspaces) {
pkg.workspaces = [Project.PACKAGE_GLOB];
}

Expand All @@ -79,14 +80,15 @@ class InitCommand extends Command {
this.logger.info("", "Updating package.json");

chain = chain.then(() => {
if (!this.hasExistingLernaConfig && !this.project.manifest.get("workspaces")) {
if (useWorkspaces && !this.project.manifest.get("workspaces")) {
this.project.manifest.set("workspaces", [Project.PACKAGE_GLOB]);

return this.project.manifest.serialize();
}
});
}

// add dependencies to package.json
chain = chain.then(() => {
const rootPkg = this.project.manifest;

Expand All @@ -108,64 +110,57 @@ class InitCommand extends Command {

setDependency({ name: "lerna", version: this.lernaVersion });

if (!this.hasExistingLernaConfig || this.project.config.useNx) {
if (useNx) {
setDependency({ name: "nx", version: "14.4.3" });
}

return rootPkg.serialize();
});

return chain;
}
chain = chain.then(() => {
let version;

if (this.options.independent) {
version = "independent";
} else if (this.project.version) {
version = this.project.version;
} else {
version = "0.0.0";
}

ensureLernaConfig() {
// config already defaulted to empty object in Project constructor
const { config, version: projectVersion } = this.project;
if (!hasExistingLernaConfig) {
this.logger.info("", "Creating lerna.json");
} else {
this.logger.info("", "Updating lerna.json");

let version;
if (!useWorkspaces && !this.project.config.packages) {
Object.assign(this.project.config, {
packages: [Project.PACKAGE_GLOB],
});
}
}

if (this.options.independent) {
version = "independent";
} else if (projectVersion) {
version = projectVersion;
} else {
version = "0.0.0";
}
delete this.project.config.lerna; // no longer relevant

let useNx = config.useNx === true;
let useWorkspaces = config.useWorkspaces === true;
if (this.exact) {
// ensure --exact is preserved for future init commands
const commandConfig = this.project.config.command || (this.project.config.command = {});
const initConfig = commandConfig.init || (commandConfig.init = {});

if (!this.hasExistingLernaConfig) {
this.logger.info("", "Creating lerna.json");
useNx = true;
useWorkspaces = true;
} else {
this.logger.info("", "Updating lerna.json");
if (!useWorkspaces && !config.packages) {
Object.assign(config, {
packages: [Project.PACKAGE_GLOB],
});
initConfig.exact = true;
}
}

delete config.lerna; // no longer relevant

if (this.exact) {
// ensure --exact is preserved for future init commands
const commandConfig = config.command || (config.command = {});
const initConfig = commandConfig.init || (commandConfig.init = {});

initConfig.exact = true;
}
Object.assign(this.project.config, {
$schema: "node_modules/lerna/schemas/lerna-schema.json",
useNx,
useWorkspaces,
version,
});

Object.assign(config, {
$schema: "node_modules/lerna/schemas/lerna-schema.json",
useNx,
useWorkspaces,
version,
return this.project.serializeConfig();
});

return this.project.serializeConfig();
return chain;
}

ensurePackagesDir() {
Expand Down

0 comments on commit c00768e

Please sign in to comment.