UNPKG

@bomb.sh/tools

Version:

The internal dev, build, and lint CLI for Bombshell projects

1 lines 2.5 kB
{"version":3,"file":"init.mjs","names":[],"sources":["../../src/commands/init.ts"],"sourcesContent":["import { readFile, writeFile } from 'node:fs/promises';\nimport { cwd } from 'node:process';\nimport { pathToFileURL } from 'node:url';\nimport { x } from 'tinyexec';\nimport type { CommandContext } from '../context.ts';\n\nexport async function init(ctx: CommandContext) {\n\tconst [_name = '.'] = ctx.args;\n\tconst cwdUrl = pathToFileURL(`${cwd()}/`);\n\t// `.` scaffolds into the current directory; otherwise clone into a new `./<name>/`.\n\tconst inPlace = _name === '.';\n\tconst name = inPlace\n\t\t? decodeURIComponent(cwdUrl.pathname.split('/').filter(Boolean).pop() ?? '.')\n\t\t: _name;\n\tconst target = inPlace ? '.' : name;\n\tconst dest = inPlace ? cwdUrl : new URL(`./${name}/`, cwdUrl);\n\tconst gigetArgs = ['giget@latest', 'gh:bombshell-dev/template', target];\n\t// `--force` lets the template extract into an existing (non-empty) directory.\n\tif (inPlace) gigetArgs.push('--force');\n\tfor await (const line of x('pnpx', gigetArgs)) {\n\t\tconsole.info(line);\n\t}\n\n\tconst promises: Promise<void>[] = [];\n\tfor (const file of ['package.json', 'README.md']) {\n\t\tpromises.push(\n\t\t\tpostprocess(new URL(file, dest), (contents) => {\n\t\t\t\treturn contents.replaceAll('$name', name);\n\t\t\t}),\n\t\t);\n\t}\n\tawait Promise.all(promises);\n}\n\nasync function postprocess(\n\tfile: URL,\n\ttransform: (contents: string) => string | undefined | Promise<string | undefined>,\n) {\n\tconst contents = await readFile(file, { encoding: 'utf8' });\n\tconst result = await transform(contents);\n\tif (!result) return;\n\tawait writeFile(file, result, { encoding: 'utf8' });\n}\n"],"mappings":";;;;;AAMA,eAAsB,KAAK,KAAqB;CAC/C,MAAM,CAAC,QAAQ,OAAO,IAAI;CAC1B,MAAM,SAAS,cAAc,GAAG,IAAI,EAAE,EAAE;CAExC,MAAM,UAAU,UAAU;CAC1B,MAAM,OAAO,UACV,mBAAmB,OAAO,SAAS,MAAM,GAAG,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,IAAI,KAAK,GAAG,IAC1E;CACH,MAAM,SAAS,UAAU,MAAM;CAC/B,MAAM,OAAO,UAAU,SAAS,IAAI,IAAI,KAAK,KAAK,IAAI,MAAM;CAC5D,MAAM,YAAY;EAAC;EAAgB;EAA6B;CAAM;CAEtE,IAAI,SAAS,UAAU,KAAK,SAAS;CACrC,WAAW,MAAM,QAAQ,EAAE,QAAQ,SAAS,GAC3C,QAAQ,KAAK,IAAI;CAGlB,MAAM,WAA4B,CAAC;CACnC,KAAK,MAAM,QAAQ,CAAC,gBAAgB,WAAW,GAC9C,SAAS,KACR,YAAY,IAAI,IAAI,MAAM,IAAI,IAAI,aAAa;EAC9C,OAAO,SAAS,WAAW,SAAS,IAAI;CACzC,CAAC,CACF;CAED,MAAM,QAAQ,IAAI,QAAQ;AAC3B;AAEA,eAAe,YACd,MACA,WACC;CAED,MAAM,SAAS,MAAM,UAAU,MADR,SAAS,MAAM,EAAE,UAAU,OAAO,CAAC,CACnB;CACvC,IAAI,CAAC,QAAQ;CACb,MAAM,UAAU,MAAM,QAAQ,EAAE,UAAU,OAAO,CAAC;AACnD"}