Skip to content

Commit

Permalink
chore: refactor tools (angular#743)
Browse files Browse the repository at this point in the history
* chore: refactor tools

* chore: fix
  • Loading branch information
lacolaco authored Sep 3, 2022
1 parent 72010bd commit 5ad9987
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.15.0
16.17.0
4 changes: 2 additions & 2 deletions aio-ja/tools/transforms/templates/error/error.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ <h1>{$ doc.code $}: {$ doc.shortDescription $}</h1>
{% if doc.videoCaption %}{$ doc.videoCaption | marked $}{% endif%}

<div class="error-description">
<h2>説明</h2>
<h2>Description</h2>
{$ doc.description | marked $}
</div>

<br>

<div class="debugging">
<h2>エラーのデバッグ</h2>
<h2>Debugging the error</h2>
{$ doc.debugging | marked $}
</div>

Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
"license": "MIT",
"private": true,
"scripts": {
"build": "zx tools/build.mjs",
"lint": "yarn ~~textlint",
"lint:fix": "yarn ~~textlint --fix",
"start": "zx tools/watch.mjs",
"build": "zx tools/build.mjs",
"lint": "yarn ~textlint",
"~textlint": "textlint 'aio-ja/**/!(*.en).md'",
"test": "npm run lint && yarn test:patch",
"test:patch": "git apply -v --check --directory origin ./scripts/git-patch/*.patch",
"update-origin": "./update-origin.sh",
"~~textlint": "textlint 'aio-ja/**/!(*.en).md'"
"test:patch": "git apply -v --check --directory origin ./tools/git-patch/*.patch",
"update-origin": "zx tools/update-origin.mjs"
},
"devDependencies": {
"chokidar": "3.5.3",
"glob": "^8.0.3",
"prh-rules": "prh/rules",
"textlint": "^12.2.1",
"textlint-filter-rule-comments": "^1.2.2",
Expand Down
56 changes: 0 additions & 56 deletions scripts/sync-origin.js

This file was deleted.

2 changes: 1 addition & 1 deletion tools/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ try {
}

async function setup() {
await resetBuildDir();
await resetBuildDir({ removeExisting: true });
}

async function preBuild() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 7 additions & 4 deletions tools/lib/common.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { watch } from 'chokidar';
import { resolve } from 'node:path';
import { $, cd, glob, within } from 'zx';
import { clearDir, cpRf, sed } from './fileutils.mjs';
import { initDir, cpRf, exists, sed } from './fileutils.mjs';

const rootDir = resolve(__dirname, '../');
const aiojaDir = resolve(rootDir, 'aio-ja');
Expand All @@ -11,8 +11,11 @@ const outDir = resolve(rootDir, 'build');
// https://github.com/google/zx/blob/main/src/util.ts#L31
$.quote = (s) => s;

export async function resetBuildDir() {
await clearDir(outDir);
export async function resetBuildDir({ removeExisting = false }) {
const buildDirExists = await exists(outDir);
if (!buildDirExists || removeExisting) {
await initDir(outDir);
}
await cpRf(resolve(rootDir, 'origin'), outDir);
}

Expand Down Expand Up @@ -66,7 +69,7 @@ export async function watchLocalizedFiles(signal) {
export async function applyPatches() {
await within(async () => {
cd(outDir);
const patches = await glob('scripts/git-patch/*.patch', { cwd: rootDir });
const patches = await glob('tools/git-patch/*.patch', { cwd: rootDir });
for (const patch of patches) {
const path = resolve(rootDir, patch);
await $`git apply -p1 --ignore-whitespace ${path}`;
Expand Down
10 changes: 8 additions & 2 deletions tools/lib/fileutils.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { access, cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';

export async function rmrf(path) {
try {
await rm(path, { recursive: true });
} catch {}
}

export async function clearDir(path) {
export async function exists(path) {
return await access(path)
.then(() => true)
.catch(() => false);
}

export async function initDir(path) {
await rmrf(path);
await mkdir(path, { recursive: true });
}
Expand Down
65 changes: 65 additions & 0 deletions tools/update-origin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { access, copyFile } from 'node:fs/promises';
import { extname, resolve } from 'node:path';
import { $, argv, chalk, glob } from 'zx';

const copyTargets = [
// Text contents
'content/cli/**/*.md',
'content/guide/**/*.md',
'content/errors/**/*.md',
'content/marketing/**/*',
'content/start/**/*.md',
'content/tutorial/**/*.md',
'content/navigation.json',
// AIO source code files
'content/examples/toh-pt6/src/app/hero-search/hero-search.component.ts',
'content/examples/toh-pt6/src/app/heroes/heroes.component.html',
'content/examples/toh-pt6/src/app/hero.service.ts',
'tools/transforms/templates/error/error.template.html',
];

try {
console.log(chalk.cyan('Checking aio changes in origin...'));

const [aioHash] = argv._;
if (aioHash == null) {
console.log('No aio origin SHA is provided.');
process.exit(1);
}
console.log(chalk.green('aio origin SHA: ' + aioHash));

await resetOriginHead(aioHash);
await copyOriginFiles();

console.log(chalk.cyan('Done.'));
} catch (err) {
console.error(chalk.red(err));
process.exit(1);
}

async function resetOriginHead(hash) {
await $`git -C origin fetch --all`;
await $`git -C origin reset ${hash} --hard`;
}

async function copyOriginFiles() {
const aioOriginDir = 'origin/aio';
const aioJaDir = 'aio-ja';

const files = await glob(copyTargets, { cwd: aioOriginDir });

for (const file of files) {
const src = resolve(aioOriginDir, file);
const ext = extname(file);
const enFilePath = file.replace(`${ext}`, `.en${ext}`);

let isTranslated = false;
try {
await access(resolve(aioJaDir, enFilePath));
isTranslated = true;
} catch {}
const dest = resolve(aioJaDir, isTranslated ? enFilePath : file);

await copyFile(src, dest);
}
}
15 changes: 11 additions & 4 deletions tools/watch.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env zx

import { chalk } from 'zx';
import { argv, chalk } from 'zx';
import { applyPatches, copyLocalizedFiles, resetBuildDir, watchAIO, watchLocalizedFiles } from './lib/common.mjs';

try {
const { 'clear-cache': clearCache = false } = argv;

console.log(chalk.green('==== setup ===='));
await setup();
await setup({ clearCache });
console.log(chalk.green('==== preWatch ===='));
await preWatch();
console.log(chalk.green('==== watch ===='));
Expand All @@ -15,11 +17,16 @@ try {
process.exit(1);
}

async function setup() {
async function setup({ clearCache }) {
console.log('');
console.log(chalk.yellow('変更監視の対象は、aio-ja 内のファイル と build/aio 内のソースコードです。'));
if (clearCache) {
console.log(chalk.yellow('build ディレクトリを初期化し、キャッシュを破棄します。'));
} else {
console.log(chalk.yellow('build ディレクトリを初期化するには --clear-cache オプションを指定してください。'));
}
console.log('');
await resetBuildDir();
await resetBuildDir({ removeExisting: clearCache });
}

async function preWatch() {
Expand Down
18 changes: 0 additions & 18 deletions update-origin.sh

This file was deleted.

25 changes: 0 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,6 @@ brace-expansion@^1.1.7:
balanced-match "^1.0.0"
concat-map "0.0.1"

brace-expansion@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.1.tgz#1edc459e0f0c548486ecf9fc99f2221364b9a0ae"
integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==
dependencies:
balanced-match "^1.0.0"

braces@^3.0.2, braces@~3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107"
Expand Down Expand Up @@ -638,17 +631,6 @@ glob@^7.1.3, glob@^7.2.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^8.0.3:
version "8.0.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-8.0.3.tgz#415c6eb2deed9e502c68fa44a272e6da6eeca42e"
integrity sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^5.0.1"
once "^1.3.0"

globby@^13.1.2:
version "13.1.2"
resolved "https://registry.yarnpkg.com/globby/-/globby-13.1.2.tgz#29047105582427ab6eca4f905200667b056da515"
Expand Down Expand Up @@ -1114,13 +1096,6 @@ minimatch@^3.1.1:
dependencies:
brace-expansion "^1.1.7"

minimatch@^5.0.1:
version "5.1.0"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.0.tgz#1717b464f4971b144f6aabe8f2d0b8e4511e09c7"
integrity sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg==
dependencies:
brace-expansion "^2.0.1"

minimist@^1.2.6:
version "1.2.6"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
Expand Down

0 comments on commit 5ad9987

Please sign in to comment.