pixture
Version:
Pixture CLI
49 lines (37 loc) • 1.3 kB
JavaScript
const fs = require('fs')
const fspath = require('path')
function loadConfig(path) {
try {
return JSON.parse(fs.readFileSync(path, { encoding: 'UTF-8' }))
}
catch (e) {
console.error(`NPM config "${path}" not found.`)
return {}
}
}
function link(names, moduleDir, localDir) {
for (const name of names) {
const modulePath = fspath.join(moduleDir, name)
const localPath = fspath.join(localDir, fspath.basename(name))
if (!fs.existsSync(localPath)) {
fs.symlinkSync(fspath.relative(localDir, modulePath), localPath)
console.log(`Create symlink "${localPath}".`)
}
}
}
function linkDependencyModules(localDir) {
if (!fs.existsSync(localDir)) {
fs.mkdirSync(localDir)
}
const config = loadConfig('./package.json')
for (const packageName in config.dependencies || {}) {
const configPath = require.resolve(fspath.join(packageName, 'package.json'))
const config = loadConfig(configPath)
const moduleDir = fspath.dirname(configPath)
if (config.main && config.main.endsWith('.mjs')) {
link([config.main, config.types].filter(_ => _), moduleDir, localDir)
}
}
}
linkDependencyModules('./modules')