frontity
Version:
Frontity cli and entry point to other packages
82 lines (80 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.installPackage = exports.createSrcIndexJs = exports.createPackageJson = void 0;
const os_1 = require("os");
const path_1 = require("path");
const fs_extra_1 = require("fs-extra");
const child_process_1 = require("child_process");
const util_1 = require("util");
const utils_1 = require("../utils");
/**
* This function creates a `package.json` file.
*
* @param name - The package name.
* @param namespace - Namespace.
* @param projectPath - Project path.
* @param packagePath - Package path.
*/
const createPackageJson = async (name, namespace, projectPath, packagePath) => {
// Get the latest version of Frontity from NPM registry
const frontityVersion = await (0, utils_1.fetchPackageVersion)("frontity");
const filePath = (0, path_1.resolve)(projectPath, packagePath, "package.json");
const fileData = `{
"name": "${name}",
"version": "1.0.0",
"description": "Frontity package created using the Frontity CLI.",
"keywords": [
"frontity",
"frontity-${namespace}"
],
"license": "Apache-2.0",
"dependencies": {
"frontity": "^${frontityVersion}"
}
}${os_1.EOL}`;
await (0, fs_extra_1.writeFile)(filePath, fileData);
};
exports.createPackageJson = createPackageJson;
/**
* This function creates an `index.js` file.
*
* @param name - The name.
* @param namespace - Namespace name.
* @param projectPath - Project path.
* @param packagePath - Package path.
*/
const createSrcIndexJs = async (name, namespace, projectPath, packagePath) => {
const filePath = (0, path_1.resolve)(projectPath, packagePath, "src/index.js");
const fileData = `const Root = () => {
return (
<>
You can edit your package in:
<pre>${(0, path_1.join)(packagePath, "src/index.js")}</pre>
</>
);
};
export default {
name: "${name}",
roots: {
${namespace}: Root
},
state: {
${namespace}: {}
},
actions: {
${namespace}: {}
}
};${os_1.EOL}`;
await (0, fs_extra_1.writeFile)(filePath, fileData);
};
exports.createSrcIndexJs = createSrcIndexJs;
/**
* This function executes the `npm i` command to add the created package.
*
* @param projectPath - Project path.
* @param packagePath - Package path.
*/
const installPackage = async (projectPath, packagePath) => {
await (0, util_1.promisify)(child_process_1.exec)(`npm install ./${packagePath}`, { cwd: projectPath });
};
exports.installPackage = installPackage;