glass-app-manager
Version:
Informatica's Glass Framework CLI for bootstrapping
142 lines (117 loc) • 4.68 kB
JavaScript
;
const globby = require("globby");
const mkdirp = require("make-dir");
const ora = require("ora");
const path = require("path");
const pEachSeries = require("p-each-series");
const chalk = require("chalk");
const copyTemplateFile = require("../common/copyTemplateFile");
const executeShellCommand = require("../common/executeShellCommand");
const replaceProductNameInFiles = require("./replaceProductNameInFiles");
const deleteProductDirectory = require("./deleteProductDirectory");
const fs = require("fs");
const renameFile = require("../common/renameFile");
const copySourceToTarget = require("../common/copySourceToDest");
module.exports = async info => {
const { name, templatePath, samplePath } = info;
const parts = name.split("/");
info.shortName = parts[parts.length - 1];
const dest = path.join(process.cwd(), info.shortName);
info.dest = dest;
await mkdirp(dest);
const source = path.join(__dirname, templatePath);
const files = await globby(source, { dot: true });
// product folder src/product
const productDest = path.join(dest, "src/", info.name);
// find out the action by checking folder contents:
let action = "create"; // by default it's create
let actionMessage = `Creating new project at ${dest}`;
// check if product folder is present
if (fs.existsSync(productDest)) {
action = "update";
actionMessage = `Updating existing project at ${dest}`;
}
console.log(chalk.cyan(actionMessage));
// delete all the folders before proceeding
// in case of create, except node_modules, all the folders and files will be deleted
// in case of update, except node_modules & src/product/ all the folders will be deleted
await deleteProductDirectory(dest, info.name, action);
console.log(`Copying project from ${source}`);
const promise = pEachSeries(files, async file => {
return copyTemplateFile({ file, source, dest, info });
});
ora.promise(promise, `Copying Glass framework app to ${dest}`);
await promise;
if ("create" === action.toLowerCase()) {
//Create product folder
await mkdirp(productDest);
console.log(
`${chalk.cyan("creating new project folder at " + productDest)}`
);
// if create then copy sample workspace inside product folder.
const sampleSourcePath = path.join(__dirname, samplePath);
await copySourceToTarget(sampleSourcePath, productDest);
// after copying is done. Rename sample.js to product.js file
await renameFile(productDest, "sample.js", `${info.name}.js`);
// rename config.json to product.json
await renameFile(productDest, "config.json", `${info.name}.json`);
// update productname in product & config file
const files = [
path.join(productDest, info.name + ".js"),
path.join(productDest, info.name + ".json"),
path.join(productDest, "dev-config.js"),
path.join(productDest, "lib", "Constants.js")
];
// config files to update with $productname
// this will be export name of the entry point, so if info.name has '-', will be changed to without '-'
const tempProductName = info.name.split("-").join(""); // this name will be used in product.js file as export name
const tempInfo = {
...info,
name: tempProductName
};
const tempConfig = {
files: files,
info: tempInfo
};
replaceProductNameInFiles(tempConfig);
} else if ("update" === action.toLowerCase()) {
console.log(
`${chalk.cyan(
"Action is " +
action +
", overwriting all the files inside folder " +
dest
)}`
);
console.log(
`${chalk.cyan(
"Product folder inside the src/" +
info.name +
" will not be deleted or overwritten"
)}`
);
}
//If info is present implies we are extracting the files.Replace $productName parameter in configuration files
if (info) {
// config files to update with $productname
const files = [
path.join(dest, "config/consumer.webpack.config.js"),
path.join(dest, "config/paths.js")
];
replaceProductNameInFiles({ files, info });
}
console.log(
`Glass framework app has been successfully copied to ${chalk.cyan(dest)}`
);
console.log(
`${
info.name
} Product files and configurations have successfully been added to ${chalk.cyan(
dest
)}`
);
// temporary fix until we publish to @informatica repo
renameFile(dest, "tempnpmrc", ".npmrc");
executeShellCommand({ dest, info });
return dest;
};