UNPKG

create-nttb

Version:

An opinionated Next.js, TypeScript, and Tailwind boilerplate using Atomic Design Methodology for presentation components.

102 lines 4.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runCommand = runCommand; exports.getPackageJson = getPackageJson; exports.getTemplatePackageJson = getTemplatePackageJson; exports.getCreateNttbVersion = getCreateNttbVersion; exports.mergeDependencies = mergeDependencies; exports.updatePackageJsonFromTemplate = updatePackageJsonFromTemplate; exports.main = main; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const child_process_1 = require("child_process"); function runCommand(command, args = [], options = {}) { const result = (0, child_process_1.spawnSync)(command, args, { stdio: "inherit", ...options }); if (!result || typeof result !== "object") { throw new Error(`${command} failed`); } if (result.error) { throw new Error(result.error.message || `${command} failed`); } if (result.status !== 0) { throw new Error(`${command} failed`); } } function getPackageJson(projectPath) { const packageJsonPath = path_1.default.join(projectPath, "package.json"); if (!fs_1.default.existsSync(packageJsonPath)) { throw new Error(`No package.json found in "${projectPath}".`); } return JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf8")); } function getTemplatePackageJson() { const templatePackageJsonPath = path_1.default.join(__dirname, "..", "template", "package.json"); if (!fs_1.default.existsSync(templatePackageJsonPath)) { throw new Error("No template package.json found."); } return JSON.parse(fs_1.default.readFileSync(templatePackageJsonPath, "utf8")); } function getCreateNttbVersion() { const rootPackageJsonPath = path_1.default.join(__dirname, "..", "package.json"); const rootPkg = JSON.parse(fs_1.default.readFileSync(rootPackageJsonPath, "utf8")); if (!rootPkg.version) { throw new Error("Could not determine create-nttb version from root package.json."); } return rootPkg.version; } function mergeDependencies(currentDeps = {}, templateDeps = {}) { return { ...currentDeps, ...templateDeps, }; } function updatePackageJsonFromTemplate(projectPath) { const packageJsonPath = path_1.default.join(projectPath, "package.json"); const currentPkg = getPackageJson(projectPath); const templatePkg = getTemplatePackageJson(); const createNttbVersion = getCreateNttbVersion(); const updated = { ...currentPkg, private: true, createNttbVersion, scripts: { ...(currentPkg.scripts ?? {}), ...(templatePkg.scripts ?? {}), }, dependencies: mergeDependencies(currentPkg.dependencies, templatePkg.dependencies), devDependencies: mergeDependencies(currentPkg.devDependencies, templatePkg.devDependencies), engines: { ...(currentPkg.engines ?? {}), ...(templatePkg.engines ?? {}), }, }; fs_1.default.writeFileSync(packageJsonPath, JSON.stringify(updated, null, 2) + "\n"); } async function main(targetDir) { const projectPath = targetDir ? path_1.default.resolve(process.cwd(), targetDir) : process.cwd(); const pkg = getPackageJson(projectPath); if (!pkg.createNttbVersion) { throw new Error(`This does not look like a create-nttb project: "${projectPath}". Missing createNttbVersion in package.json.`); } console.log(`Updating package.json from template in ${projectPath}...`); updatePackageJsonFromTemplate(projectPath); console.log("Installing updated packages..."); runCommand("npm", ["install"], { cwd: projectPath }); const updatedPkg = getPackageJson(projectPath); const scripts = updatedPkg.scripts ?? {}; if (scripts.lint) { console.log("Running lint..."); runCommand("npm", ["run", "lint"], { cwd: projectPath }); } if (scripts.build) { console.log("Running build..."); runCommand("npm", ["run", "build"], { cwd: projectPath }); } console.log("Update complete."); } //# sourceMappingURL=update.js.map