Skip to content

Commit 51b4545

Browse files
authored
Merge pull request #7 from code4rena-dev/nlf/update-code-skeleton
feat: update to code-skeleton@2
2 parents e9b06b8 + ca9ad44 commit 51b4545

File tree

3 files changed

+24
-51
lines changed

3 files changed

+24
-51
lines changed

lib/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ export default async function (root: string, variables: Variables) {
9898
".eslintrc.js": copy(join(__dirname, "content", "eslintrc.js")),
9999
".gitignore": copy(join(__dirname, "content", "gitignore")),
100100
"scripts/clean.ts": copy(join(__dirname, "content", "clean.ts")),
101-
".github/workflows/ci.yml": mustache(join(__dirname, "content", "ci.yml"), variables.ci),
101+
".github/workflows/ci.yml": mustache({
102+
sourcePath: join(__dirname, "content", "ci.yml"),
103+
variables,
104+
}),
102105
".github/matchers/tap.json": copy(join(__dirname, "content", "tap.json")),
103106
};
104107

lib/mustache.ts

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import {
2-
Generator,
3-
GeneratorResults,
4-
GeneratorOptions
5-
} from "code-skeleton/lib/generators/abstract";
6-
import { dirname } from "node:path";
7-
import { writeFile, mkdir, readFile } from "node:fs/promises";
1+
import { Generator } from "code-skeleton/lib/generators/abstract";
2+
import { readFile } from "node:fs/promises";
83
import Mustache from "mustache";
94
Mustache.tags = [ "<%", "%>" ];
105

11-
interface MustacheGeneratorOptions extends GeneratorOptions {
6+
interface MustacheGeneratorOptions {
127
sourcePath: string;
8+
variables: unknown;
139
}
1410

15-
class MustacheGenerator extends Generator {
11+
class MustacheGenerator extends Generator<MustacheGeneratorOptions> {
1612
declare options: MustacheGeneratorOptions;
1713

1814
constructor (options: MustacheGeneratorOptions) {
@@ -23,47 +19,14 @@ class MustacheGenerator extends Generator {
2319
}
2420
}
2521

26-
async apply(targetPath: string): Promise<GeneratorResults> {
27-
await mkdir(dirname(targetPath), { recursive: true });
28-
try {
29-
const source = await readFile(this.options.sourcePath);
30-
31-
const rendered = Mustache.render(source.toString(), this.options);
32-
await writeFile(targetPath, rendered);
33-
return this.pass();
34-
} catch (err) {
35-
const { code, message } = err as { code?: string; message: string };
36-
// istanbul ignore next - no need to test message fallback
37-
return this.fail(code ?? message);
38-
}
39-
}
40-
async verify(targetPath: string): Promise<GeneratorResults> {
41-
let actual;
42-
try {
43-
actual = await readFile(targetPath);
44-
} catch (err) {
45-
const { code, message } = err as { code?: string; message: string };
46-
// istanbul ignore next - no need to test passthrough throws
47-
if (code !== "ENOENT") {
48-
return this.fail(code ?? message);
49-
}
50-
51-
return this.fail("file missing");
52-
}
53-
22+
async generate () {
5423
const source = await readFile(this.options.sourcePath);
55-
const expected = Buffer.from(Mustache.render(source.toString(), this.options));
56-
if (actual.compare(expected) === 0) {
57-
return this.pass();
58-
}
5924

60-
return this.fail("contents do not match");
25+
const rendered = Mustache.render(source.toString(), this.options.variables);
26+
return rendered;
6127
}
6228
}
6329

64-
export function mustache (sourcePath: string, options: GeneratorOptions = {}) {
65-
return new MustacheGenerator({
66-
...options,
67-
sourcePath
68-
});
69-
}
30+
export function mustache (options: MustacheGeneratorOptions) {
31+
return new MustacheGenerator(options);
32+
}

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"prelint": "tsc --noEmit",
1313
"postlint": "npm run skeleton:verify",
1414
"skeleton:apply": "code-skeleton apply",
15+
"preskeleton:verify": "npm run prepack",
1516
"skeleton:verify": "code-skeleton verify"
1617
},
1718
"keywords": [],
@@ -25,13 +26,13 @@
2526
"@typescript-eslint/eslint-plugin": "^5.0.0",
2627
"@typescript-eslint/parser": "^5.0.0",
2728
"eslint": "^8.0.0",
28-
"mustache": "^4.0.0",
29+
"mustache": "^4.2.0",
2930
"tap": "^16.0.0",
3031
"ts-node": "^10.0.0",
3132
"typescript": "^5.0.0"
3233
},
3334
"peerDependencies": {
34-
"code-skeleton": "^1.0.0"
35+
"code-skeleton": "^2.0.0"
3536
},
3637
"skeleton": {
3738
"module": ".",
@@ -43,6 +44,12 @@
4344
}
4445
}
4546
},
47+
"//": "This file is partially managed by code-skeleton. Changes may be overwritten.",
48+
"tap": {
49+
"coverage": true,
50+
"ts": true
51+
},
52+
"types": "lib/index.d.ts",
4653
"files": [
4754
"bin/**/*.js",
4855
"lib/**/*.js",

0 commit comments

Comments
 (0)