Common base for my other typescript projects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

194 lines
3.8 KiB

#!./run
import { Sys } from "./system.ts";
/**
* Normalize deno projects
*/
async function isDirectory(path: string): Promise<boolean> {
try {
return (await Sys.stat(path)).isDirectory;
} catch {
return false;
}
}
async function readContent(path: string): Promise<string> {
try {
return await Sys.readTextFile(path);
} catch {
return "";
}
}
async function ensureDirectory(path: string) {
if (!await isDirectory(path)) {
await Sys.mkdir(path, { recursive: true });
}
}
async function updateRunScript() {
await Sys.writeTextFile(
"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
`,
);
await Sys.chmod("run", 0o755);
}
async function updateDenoDefs() {
const process = Sys.run({
cmd: ["./run", "types"],
stdout: "piped",
});
const defs = new TextDecoder("utf-8").decode(await process.output());
await Sys.writeTextFile("deno.d.ts", defs);
}
async function updateEditorConfig() {
await Sys.writeTextFile(
".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
`,
);
}
async function updateTsConfig() {
await Sys.writeTextFile(
"tsconfig.json",
`{
"compilerOptions": {
"module": "esnext",
"target": "ESNext",
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"preserveConstEnums": true
}
}
`,
);
}
async function updateVscodeConf() {
await ensureDirectory(".vscode");
const path = ".vscode/settings.json";
const json_config = await readContent(path);
const config = JSON.parse(json_config || "{}");
if (!config["deno.enable"]) {
config["deno.enable"] = true;
await Sys.writeTextFile(path, JSON.stringify(config));
await Sys.run({
cmd: ["./run", "fmt", "-q", path],
}).status();
}
await Sys.writeTextFile(
".vscode/tasks.json",
`{
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
},
"command": "./run test"
}
]
}
`,
);
}
async function updateGitIgnore() {
await Sys.writeTextFile(
".gitignore",
`deno.d.ts
.vscode
.local
`,
);
}
async function updateGitHooks() {
await Sys.writeTextFile(
".git/hooks/pre-commit",
`#!/bin/sh
set -e
./run fmt --check
./run test
`,
);
await Sys.chmod(".git/hooks/pre-commit", 0o755);
}
async function updateReadme() {
const project = Sys.cwd().split("/").pop();
let sections = "";
if (await isDirectory("doc")) {
const index = await readContent("doc/index");
for (let section of index.split("\n")) {
if (section?.trim()) {
sections += "\n" + (await readContent(`doc/${section.trim()}.md`));
}
}
}
await Sys.writeTextFile(
"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}`,
);
await Sys.run({
cmd: ["./run", "fmt", "-q", "README.md"],
}).status();
}
export async function normalize() {
if (!await isDirectory(".git")) {
throw new Error("Not in a git repository");
}
await updateRunScript();
await updateDenoDefs();
await updateVscodeConf();
await updateTsConfig();
await updateEditorConfig();
await updateGitIgnore();
await updateGitHooks();
await updateReadme();
}
if (import.meta.main) {
await normalize();
}