import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import test from "node:test"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const script = path.join(root, "scripts/template-new.cjs"); test("template:new scaffolds a package with manifest, renderer and an explicit incomplete status", () => { const temporary = mkdtempSync(path.join(os.tmpdir(), "template-new-test-")); const packagesDirectory = path.join(temporary, "packages"); const isolatedScript = path.join(temporary, "scripts/template-new.cjs"); mkdirSync(path.dirname(isolatedScript), { recursive: true }); mkdirSync(packagesDirectory, { recursive: true }); cpSync(script, isolatedScript); const source = readFileSync(script, "utf8").replace( 'const root = path.resolve(__dirname, "..");', `const root = ${JSON.stringify(temporary)};`, ); const patchedScript = path.join(temporary, "scripts/template-new-isolated.cjs"); writeFileSync(patchedScript, source); try { execFileSync(process.execPath, [patchedScript, "example-brand"], { encoding: "utf8" }); const directory = path.join(packagesDirectory, "template-example-brand"); const manifest = JSON.parse(readFileSync(path.join(directory, "template.json"), "utf8")); assert.equal(manifest.packageKey, "example-brand"); assert.match(manifest.supportedIndustryTemplates[0], /^replace-with-/); assert.match(readFileSync(path.join(directory, "README.md"), "utf8"), /Scaffold only/); assert.match(readFileSync(path.join(directory, "taro/index.tsx"), "utf8"), /TemplatePage/); const duplicate = spawnSync(process.execPath, [patchedScript, "example-brand"], { encoding: "utf8", }); assert.equal(duplicate.status, 1); } finally { rmSync(temporary, { recursive: true, force: true }); } });