create-strof
Version:
The npm-initializer for creating a new strof directoty or repository
288 lines (279 loc) • 8.05 kB
JavaScript
// src/create-strof-cli.ts
import * as prom from "@clack/prompts";
import chalk from "chalk";
// package.json
var package_default = {
name: "create-strof",
version: "0.1.12",
description: "The npm-initializer for creating a new strof directoty or repository",
private: false,
repository: {
type: "git",
url: "git+https://github.com/charlyoleg2/strof_mono.git"
},
homepage: "https://www.npmjs.com/package/create-strof",
author: "charlyoleg",
license: "ISC",
keywords: [
"strof",
"strophe",
"paragraph",
"sbook",
"expert system"
],
type: "module",
exports: {
".": {
types: "./dist/create-strof-api.d.ts",
default: "./dist/create-strof-api.js"
}
},
bin: {
"create-strof": "dist/create-strof-cli.js"
},
files: [
"dist/create-strof-cli.js",
"dist/template/",
"!dist/**/*.map",
"!dist/**/*.spec.*"
],
tsup: {
entry: [
"src/create-strof-api.ts",
"src/create-strof-ref.ts",
"src/create-strof-cli.ts"
],
format: "esm",
splitting: false,
dts: false,
sourcemap: false,
clean: true
},
prettier: {
useTabs: true,
singleQuote: true,
trailingComma: "none",
printWidth: 100,
plugins: [],
overrides: []
},
scripts: {
dev: "tsup --watch",
build: "tsup",
check: "tsc --noEmit",
pretty: "prettier --check .",
format: "prettier --write .",
lint: "eslint .",
"test:unit": "vitest",
"test:unit:once": "vitest --run",
copy_template: "shx cp -r template dist/",
cleanCopy_template: "run-s clean:template copy_template",
ci: "run-s check build pretty lint test:unit:once cleanCopy_template",
run: "dist/create-strof-cli.js",
"run:ref": "dist/create-strof-ref.js tmp2",
"run:diff": "diff -rq tmp tmp2",
"run:check": "run-s run:ref run:diff",
cycle: "run-s clean ci run",
"clean:template": "shx rm -fr dist/template",
"clean:build": "shx rm -fr dist node_modules",
"clean:output": "shx rm -fr tmp tmp2",
clean: "run-s clean:build clean:output"
},
dependencies: {
"@clack/prompts": "^0.11.0",
chalk: "^5.6.2",
handlebars: "^4.7.8"
},
devDependencies: {
"@eslint/js": "^9.10.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^25.0.3",
eslint: "^9.39.2",
"eslint-config-prettier": "^10.1.8",
"npm-run-all2": "^8.0.4",
prettier: "^3.7.4",
shx: "^0.4.0",
tsup: "^8.5.1",
typescript: "^5.9.3",
"typescript-eslint": "^8.50.1",
vitest: "^4.0.16"
}
};
// src/create-strof-cli.ts
import { setTimeout as sleep2 } from "timers/promises";
// src/create-strof-api.ts
import { setTimeout as sleep } from "timers/promises";
import { readFile, writeFile, access, mkdir } from "fs/promises";
import { dirname, extname } from "path";
import Handlebars from "handlebars";
// src/create-strof-common.ts
function prefixOutputPath() {
let rPreDir = ".";
const scriptDir = new URL("", import.meta.url).toString();
const regex = new RegExp("/node_modules/");
if (!regex.test(scriptDir)) {
rPreDir = "./tmp";
}
return rPreDir;
}
var strofDir = "strofDir";
// src/create-strof-list.ts
var template_file_list = [
".editorconfig",
".gitignore",
"package.json",
"README.md",
"strof_config.js",
"gen_strof_for_nodejs.js",
"data/rep_contacts.txt",
"data/rep_business.txt",
"data/rep_familly.txt",
"data/rep_friends.txt"
];
var template_linux_file_list = ["gen_strof_for_linux_bash.js"];
var template_windows_file_list = ["gen_strof_for_windows.js"];
// src/create-strof-api.ts
async function createMissingDir(outPath) {
const outDir = dirname(outPath);
try {
await access(outDir);
} catch (err) {
if (err) {
await mkdir(outDir, { recursive: true });
}
}
}
function isFileBinary(fpath) {
const binaryExts = /* @__PURE__ */ new Set([".png"]);
const infileExt = extname(fpath.toString());
const rBool = binaryExts.has(infileExt);
return rBool;
}
async function oneFile(onePath, cfg2, preDir2) {
try {
const onePathIn = Handlebars.compile(onePath)({ linuxScript: true, windowsScript: true });
const onePathOut = Handlebars.compile(onePath)(cfg2);
const fileIn1 = new URL(`./template/${onePathIn}.handlebars`, import.meta.url);
const fileIn2 = new URL(`./template/${onePathIn}`, import.meta.url);
let fileBin = false;
let fileStr2 = "";
const outPath = `${preDir2}/${strofDir}/${onePathOut}`;
try {
await access(fileIn1);
try {
const fileStr1 = await readFile(fileIn1, { encoding: "utf8" });
const templateStr = Handlebars.compile(fileStr1);
fileStr2 = templateStr(cfg2);
} catch (err) {
console.log(`err392: error while processing ${fileIn1.toString()}`);
console.log(err);
}
} catch (err) {
if (err) {
if (isFileBinary(fileIn2)) {
fileBin = true;
const fileBuffer2 = await readFile(fileIn2);
await createMissingDir(outPath);
await writeFile(outPath, fileBuffer2);
} else {
fileStr2 = await readFile(fileIn2, { encoding: "utf8" });
}
}
}
if (!fileBin) {
await createMissingDir(outPath);
await writeFile(outPath, fileStr2);
}
} catch (err) {
console.log(`err213: error while generating file ${onePath}`);
console.error(err);
throw `err214: error with path ${onePath}`;
}
}
async function generate_boirlerplate(cfg12, preDir2) {
console.log(`Boilerplate with:
linuxScript : ${cfg12.linuxScript}
windowsScript : ${cfg12.windowsScript}`);
const cfg2 = {
linuxScript: cfg12.linuxScript,
windowsScript: cfg12.windowsScript
};
let cnt_file = 0;
for (const fpath of template_file_list) {
await oneFile(fpath, cfg2, preDir2);
cnt_file += 1;
}
if (cfg2.linuxScript) {
for (const fpath of template_linux_file_list) {
await oneFile(fpath, cfg2, preDir2);
cnt_file += 1;
}
}
if (cfg2.windowsScript) {
for (const fpath of template_windows_file_list) {
await oneFile(fpath, cfg2, preDir2);
cnt_file += 1;
}
}
console.log(`generate ${cnt_file} files in ${preDir2}/${strofDir}/`);
await sleep(100);
const rResp = {
vim: `vim data/rep_contacts.txt`
};
return rResp;
}
// src/create-strof-cli.ts
var firstMsg = `Create a new ${chalk.italic("strof")} directory with ${chalk.italic(package_default.name)} version ${chalk.italic(package_default.version)}`;
console.log(firstMsg);
var argN = process.argv.length - 2;
if (argN > 0) {
console.log(`warn376: ${argN} arguments provided but all argument are ignored!`);
}
prom.intro(chalk.inverse(" Your new strof directory "));
var pCfg = await prom.group(
{
linuxScript: () => prom.confirm({
message: "Do you want an helper Linux script?",
initialValue: true
//placeholder: `${projName}`
}),
windowsScript: () => prom.confirm({
message: "Do you want an helper Windows script?",
initialValue: false
//placeholder: `${projName}_vag`
})
},
{
onCancel: () => {
prom.cancel("Operation aborted!");
process.exit(0);
}
}
);
prom.outro("Your new strof directory will be boilerplated!");
var cfg1 = {
linuxScript: pCfg.linuxScript,
windowsScript: pCfg.windowsScript
};
var preDir = prefixOutputPath();
var resp = await generate_boirlerplate(cfg1, preDir);
await sleep2(100);
function styl(str) {
const rStr = chalk.bold.cyan(str);
return rStr;
}
var lastMsg = `
Next steps:
1: ${styl(`cd ${strofDir}`)}
2: ${styl(`npm install`)}
3: ${styl(`git init && git add -A && git commit -m "Initial commit of ${strofDir}"`)} (optional)
4: ${styl(`${resp.vim}`)} (optional)
5: ${styl(`vim strof_config.js`)} (optional)
6: ${styl(`node gen_strof_for_nodejs.js`)}
7: ${styl(`chmod +x strof.mjs`)}
8: ${styl(`mv strof.mjs ~/bin/`)}
9: ${styl(`strof.mjs tom ardennes`)}
10: ${styl(`strof.mjs tom ardennes -v champagne -a`)}
`;
console.log(lastMsg);