UNPKG

create-gojs-kit

Version:

A CLI for downloading GoJS samples, extensions, and docs

77 lines (76 loc) 2.58 kB
#!/usr/bin/env node import fs from 'node:fs'; import * as p from '@clack/prompts'; import { bold, cyan, grey } from 'kleur/colors'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; const { version } = JSON.parse(fs.readFileSync(new URL('package.json', import.meta.url), 'utf-8')); let cwd = process.argv[2] || '.'; const kitDir = 'gojs-kit'; console.log(` ${grey(`create-gojs-kit version ${version}`)} `); p.intro(`This will copy a ${cyan(kitDir)} directory containing GoJS samples and extensions.`); if (cwd === '.') { const dir = await p.text({ message: `Where should we put them?`, placeholder: ' (hit Enter to use current directory)' }); if (p.isCancel(dir)) process.exit(1); if (dir) cwd = dir; } const dirs = ['samples', 'extensions']; for (const x of dirs) { if (fs.existsSync(`${cwd}/${x}`)) { if (fs.readdirSync(`${cwd}/${x}`).length > 0) { const force = await p.confirm({ message: `${cwd}/${x} Directory not empty. Continue?`, initialValue: false }); // bail if `force` is `false` or the user cancelled with Ctrl-C if (force !== true) { process.exit(1); } } } } mkdirp(cwd); const dir = fileURLToPath(new URL(`./dist`, import.meta.url).href); copy(dir, `${cwd}/${kitDir}`); p.outro(`${bold(cyan(kitDir))} directory copied into ${(cwd === '.') ? 'current' : bold(cyan(cwd))} directory`); console.log(`The kit contains hundreds of ${cyan('samples')} and ${cyan('extensions')}, which you can use as starting points, or search for example use of properties and methods`); console.log(`\nVisit ${cyan('https://gojs.net')} for more information.`); console.log(`Or ${cyan('https://nwoods.com/support.html')} for support.`); console.log(`\nThis command does not install GoJS, only sample files. To install GoJS via npm, type ${cyan('npm i gojs -D')}`); export function mkdirp(dir) { try { fs.mkdirSync(dir, { recursive: true }); } catch (e) { if ((e).code === 'EEXIST') return; throw e; } } export function copy(from, to, rename = identity) { if (!fs.existsSync(from)) return; const stats = fs.statSync(from); if (stats.isDirectory()) { fs.readdirSync(from).forEach((file) => { copy(path.join(from, file), path.join(to, rename(file))); }); } else { mkdirp(path.dirname(to)); fs.copyFileSync(from, to); } } function identity(x) { return x; }