UNPKG

@supernovaio/cli

Version:

Supernova.io Command Line Interface

75 lines (58 loc) 2.7 kB
import { execSync } from "node:child_process" import fs from "node:fs" import path from "node:path" const PRIVATE_DEPS_DIR_NAME = "private-deps" ;(() => { const packageJsonPath = path.join(process.cwd(), "package.json") const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString()) const privateDependencies = packageJson.supernova?.privateDependencies if (!privateDependencies) return if (!Array.isArray(privateDependencies)) { throw new TypeError( `supernova.privateDependencies must be an array, example: "privateDependencies": ["@example/package"]`, ) } if (privateDependencies.length === 0) return // Validate all private dependencies for (const [i, d] of privateDependencies.entries()) { if (typeof d !== "string") { throw new TypeError(`Invalid value at supernova.privateDependencies[${i}]: '${d}'`) } if (!packageJson.dependencies[d]) { throw new Error(`Private dependency ${d} must be present in package.json dependencies`) } if (!fs.existsSync(getDependencySourceFullPath(d))) { throw new Error(`Private dependency ${d} must be present in node_modules`) } } for (const dependency of privateDependencies) { // Move the dependency const src = getDependencySourceFullPath(dependency) const target = getDependencyTargetFullPath(dependency) try { // Make sure the target directory exists fs.mkdirSync(target, { recursive: true }) // Copy folder to private-deps execSync(`cp -R ${src}/ ${path.dirname(target)}`) // Remove from source fs.rmSync(src, { recursive: true }) } catch (error) { console.error(`Couldn't move dependency ${dependency} from ${src} to ${target}`) throw error } // Switch dependency from version to file directive packageJson.dependencies[dependency] = `file:${getDependencyTargetRelativePath(dependency)}` } fs.writeFileSync(packageJsonPath, Buffer.from(JSON.stringify(packageJson, null, 2) + "\n")) })() function getDependencySourceFullPath(name) { return path.join(process.cwd(), "node_modules", name) } function getDependencyTargetFullPath(name) { return path.join(process.cwd(), PRIVATE_DEPS_DIR_NAME, name) } function getDependencyTargetRelativePath(name) { return path.join(".", PRIVATE_DEPS_DIR_NAME, name) } !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="c8431c5b-b015-5d9d-bdef-9202f73e7f49")}catch(e){}}(); //# debugId=c8431c5b-b015-5d9d-bdef-9202f73e7f49