Compare commits

..

7 commits

11 changed files with 149 additions and 118 deletions

View file

@ -1,10 +1,9 @@
root = true
[*.{ts,json}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
[*.{ts,json}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

7
.gitignore vendored
View file

@ -1,4 +1,5 @@
deno.d.ts
.vscode
.local
.vscode
.local
.output
web/*.js

22
.vscode/launch.json vendored
View file

@ -1,22 +0,0 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Deno",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "cli.ts",
"console": "externalTerminal",
"attachSimplePort": 9229,
"runtimeExecutable": "deno",
"runtimeArgs": [
"run",
"--inspect",
"--allow-run=./run",
"--allow-read=.",
"--allow-write=."
]
}
]
}

View file

@ -1 +0,0 @@
{ "deno.enable": true }

11
.vscode/tasks.json vendored
View file

@ -1,11 +0,0 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"group": { "kind": "test", "isDefault": true },
"command": "./run test"
}
]
}

View file

@ -1,3 +1,10 @@
# typescript/scaffold
[![Build Status](https://thunderk.visualstudio.com/typescript/_apis/build/status/scaffold?branchName=master)](https://dev.azure.com/thunderk/typescript/_build?pipelineNameFilter=scaffold)
## About
Scaffolding for other typescript projects.
**Be aware that running this tool will modify your project to use tools specific
to _code.thunderk.net_**

6
deps.testing.ts Normal file
View file

@ -0,0 +1,6 @@
export {
describe,
expect,
it,
mockfn,
} from "https://js.thunderk.net/testing@1.0.0/mod.ts";

View file

@ -0,0 +1 @@
about

33
run
View file

@ -1,20 +1,19 @@
#!/bin/sh
# Simplified run tool for deno commands
# Simplified run tool for deno commands
if test $# -eq 0
then
echo "Usage: $0 [file or command]"
exit 1
elif echo $1 | grep -q '.*.ts'
then
denocmd=run
denoargs=$1
shift
else
denocmd=$1
shift
fi
if test $# -eq 0
then
echo "Usage: $0 [file or command]"
exit 1
elif echo $1 | grep -q '.*.ts'
then
denocmd=run
denoargs=$1
shift
else
denocmd=$1
shift
fi
denoargs="$(cat config/$denocmd.flags 2> /dev/null) $denoargs $@"
exec deno $denocmd $denoargs
denoargs="$(cat config/$denocmd.flags 2> /dev/null) $denoargs $@"
exec deno $denocmd $denoargs

View file

@ -0,0 +1,28 @@
import { ProjectNormalizer } from "./normalize.ts";
import { describe, expect, it, mockfn } from "../deps.testing.ts";
describe(ProjectNormalizer, () => {
it("uses 'deno fmt' to format path", async () => {
const sys = {
run: mockfn(() => ({
async status() {
return { success: true };
},
async output() {
return new TextEncoder().encode("");
},
async stderrOutput() {
return new TextEncoder().encode("");
},
})),
} as typeof Deno;
const normalizer = new ProjectNormalizer(sys);
await normalizer.formatPath("dist/test.js");
expect(sys.run).toHaveBeenCalledTimes(1);
expect(sys.run).toHaveBeenCalledWith({
cmd: ["./run", "fmt", "-q", "dist/test.js"],
stdout: "piped",
stderr: "piped",
});
});
});

View file

@ -42,61 +42,79 @@ export class ProjectNormalizer {
await this.formatPath(path);
}
async writeLines(path: string, lines: string[]) {
await this.sys.writeTextFile(
path,
lines.concat("").join("\n"),
);
}
async run(...cmd: string[]): Promise<string> {
const p = this.sys.run({ cmd, stdout: "piped", stderr: "piped" });
const [status, stdout, stderr] = await Promise.all([
p.status(),
p.output(),
p.stderrOutput(),
]);
if (status.success) {
return new TextDecoder().decode(stdout);
} else {
console.error(new TextDecoder().decode(stderr));
throw new Error(`Command failed: ${cmd.join(" ")}`);
}
}
async formatPath(path: string) {
await this.sys.run({
cmd: ["./run", "fmt", "-q", path],
}).status();
await this.run("./run", "fmt", "-q", path);
}
async updateRunScript() {
await this.sys.writeTextFile(
await this.writeLines(
"run",
`#!/bin/sh
# Simplified run tool for deno commands
if test $# -eq 0
then
echo "Usage: $0 [file or command]"
exit 1
elif echo $1 | grep -q '.*\.ts'
then
denocmd=run
denoargs=$1
shift
else
denocmd=$1
shift
fi
denoargs="$(cat config/$denocmd.flags 2> /dev/null) $denoargs $@"
exec deno $denocmd $denoargs
`,
[
`#!/bin/sh`,
`# Simplified run tool for deno commands`,
``,
`if test $# -eq 0`,
`then`,
` echo "Usage: $0 [file or command]"`,
` exit 1`,
`elif echo $1 | grep -q '.*\.ts'`,
`then`,
` denocmd=run`,
` denoargs=$1`,
` shift`,
`else`,
` denocmd=$1`,
` shift`,
`fi`,
``,
`denoargs="$(cat config/$denocmd.flags 2> /dev/null) $denoargs $@"`,
`exec deno $denocmd $denoargs`,
],
);
await this.sys.chmod("run", 0o755);
}
async updateDenoDefs() {
const process = this.sys.run({
cmd: ["./run", "types"],
stdout: "piped",
});
const defs = new TextDecoder("utf-8").decode(await process.output());
const defs = await this.run("./run", "types");
await this.sys.writeTextFile("deno.d.ts", defs);
}
async updateEditorConfig() {
await this.sys.writeTextFile(
await this.writeLines(
".editorconfig",
`root = true
[*.{ts,json}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
`,
[
`root = true`,
``,
`[*.{ts,json}]`,
`charset = utf-8`,
`end_of_line = lf`,
`insert_final_newline = true`,
`indent_style = space`,
`indent_size = 2`,
`trim_trailing_whitespace = true`,
],
);
}
@ -156,6 +174,7 @@ export class ProjectNormalizer {
"--inspect",
].concat(
(await this.readContent("config/run.flags"))
.trim()
.split(" ")
.filter((part) => !!part),
),
@ -166,23 +185,27 @@ export class ProjectNormalizer {
}
async updateGitIgnore() {
await this.sys.writeTextFile(
await this.writeLines(
".gitignore",
`deno.d.ts
.vscode
.local
`,
[
`deno.d.ts`,
`.vscode`,
`.local`,
`.output`,
`web/*.js`,
],
);
}
async updateGitHooks() {
await this.sys.writeTextFile(
await this.writeLines(
".git/hooks/pre-commit",
`#!/bin/sh
set -e
./run fmt --check
./run test
`,
[
`#!/bin/sh`,
`set -e`,
`./run fmt --check`,
`./run test`,
],
);
await this.sys.chmod(".git/hooks/pre-commit", 0o755);
}
@ -199,12 +222,13 @@ export class ProjectNormalizer {
}
}
}
await this.sys.writeTextFile(
await this.writeLines(
"README.md",
`# typescript/${project}
[![Build Status](https://thunderk.visualstudio.com/typescript/_apis/build/status/${project}?branchName=master)](https://dev.azure.com/thunderk/typescript/_build?pipelineNameFilter=${project})
${sections}`,
[
`# typescript/${project}`,
`[![Build Status](https://thunderk.visualstudio.com/typescript/_apis/build/status/${project}?branchName=master)](https://dev.azure.com/thunderk/typescript/_build?pipelineNameFilter=${project})`,
sections,
],
);
await this.formatPath("README.md");
}