Skip to content

Commit 0c80de5

Browse files
committed
chore: rewrite watch mechanism
1 parent 2e79d51 commit 0c80de5

File tree

7 files changed

+244
-54
lines changed

7 files changed

+244
-54
lines changed

.bazelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Print all the options that apply to the build.
2+
# This helps us diagnose which options override others
3+
# (e.g. /etc/bazel.bazelrc vs. tools/bazel.rc)
4+
build --announce_rc
5+
6+
# More details on failures
7+
build --verbose_failures=true
8+
9+
# CI supports colors but Bazel does not detect it.
10+
common --color=yes

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.md
2+
3+
origin
4+

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
"packageManager": "yarn@1.22.10",
1919
"devDependencies": {
2020
"chokidar": "3.6.0",
21+
"execa": "^9.3.0",
2122
"prh-rules": "prh/rules",
2223
"textlint": "^14.0.4",
2324
"textlint-filter-rule-comments": "^1.2.2",
2425
"textlint-rule-prh": "^6.0.0",
26+
"tree-kill": "^1.2.2",
2527
"zx": "8.1.3"
2628
}
2729
}

tools/lib/common.mjs

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { watch } from 'chokidar';
22
import { resolve } from 'node:path';
3+
import { execa } from 'execa';
4+
// TODO: replace $ to execa
35
import { $, cd, chalk, glob, within } from 'zx';
6+
import kill from 'tree-kill';
47
import { cpRf, exists, initDir, rename, sed } from './fileutils.mjs';
58

9+
const $$ = execa({
10+
stdin: 'inherit',
11+
stdout: 'inherit',
12+
stderr: 'inherit',
13+
preferLocal: true,
14+
});
15+
616
const rootDir = resolve(__dirname, '../');
7-
const adevDir = resolve(rootDir, 'adev-ja');
17+
const adevJaDir = resolve(rootDir, 'adev-ja');
818
const aiojaDir = resolve(rootDir, 'aio-ja');
919
const outDir = resolve(rootDir, 'build');
1020

@@ -20,6 +30,8 @@ export async function resetBuildDir({ init = false }) {
2030
await initDir(outDir);
2131
console.log(chalk.cyan('copying origin files to build directory...'));
2232
await cpRf(resolve(rootDir, 'origin'), outDir);
33+
console.log(chalk.cyan('copying .bazelrc to build directory...'));
34+
await cpRf(resolve(rootDir, '.bazelrc'), resolve(outDir, '.bazelrc.user'));
2335
}
2436
}
2537

@@ -31,11 +43,22 @@ export async function buildAdev() {
3143
});
3244
}
3345

34-
export async function watchAIO() {
35-
await within(async () => {
36-
cd(`${outDir}`);
37-
await $`yarn docs`;
46+
export function serveAdev() {
47+
const p = $$({
48+
cwd: outDir,
49+
reject: false,
50+
})`npx bazel run //adev:serve --fast_adev`;
51+
console.debug(chalk.gray('adev process started.', p.pid));
52+
const abort = () => kill(p.pid);
53+
p.finally(() => {
54+
console.debug(chalk.gray('adev process exited.', p.pid));
3855
});
56+
return {
57+
cancel: async () => {
58+
abort();
59+
return await p;
60+
},
61+
};
3962
}
4063

4164
/**
@@ -45,29 +68,32 @@ const lozalizedFilePatterns = ['**/*', '!**/*.en.*', '!**/*.old'];
4568

4669
export async function copyLocalizedFiles() {
4770
const jaFiles = await glob(lozalizedFilePatterns, {
48-
cwd: aiojaDir,
71+
cwd: adevJaDir,
4972
});
5073
for (const file of jaFiles) {
51-
const src = resolve(aiojaDir, file);
52-
const dest = resolve(outDir, 'aio', file);
74+
const src = resolve(adevJaDir, file);
75+
const dest = resolve(outDir, 'adev', file);
5376
await cpRf(src, dest);
5477
}
5578
}
5679

5780
/**
58-
*
59-
* @param {AbortSignal} signal
81+
* @param {() => () => void} onChangeCallback
6082
*/
61-
export async function watchLocalizedFiles(signal) {
62-
const watcher = watch(lozalizedFilePatterns, {
63-
cwd: aiojaDir,
64-
});
65-
watcher.on('change', (path) => {
66-
const src = resolve(aiojaDir, path);
67-
const dest = resolve(outDir, 'aio', path);
68-
cpRf(src, dest);
83+
export function watchLocalizedFiles(onChangeCallback) {
84+
const watcher = watch(lozalizedFilePatterns, { cwd: adevJaDir });
85+
watcher.on('change', async (path) => {
86+
console.log(chalk.cyan(`File changed: ${path}`));
87+
const src = resolve(adevJaDir, path);
88+
const dest = resolve(outDir, 'adev', path);
89+
await cpRf(src, dest);
90+
onChangeCallback();
6991
});
70-
signal.addEventListener('abort', () => watcher.close());
92+
return {
93+
cancel: () => {
94+
watcher.close();
95+
},
96+
};
7197
}
7298

7399
export async function applyPatches() {
@@ -100,11 +126,9 @@ export async function copyRobots() {
100126
// Copy static files into build output directory
101127
export async function copyStaticFiles() {
102128
await $`chmod -R +w ${resolve(outDir, 'dist/bin/adev/build/browser')}`;
103-
const files = [
104-
'_headers',
105-
];
129+
const files = ['_headers'];
106130
for (const file of files) {
107-
const src = resolve(adevDir, file);
131+
const src = resolve(adevJaDir, file);
108132
const dest = resolve(outDir, 'dist/bin/adev/build/browser', file);
109133
await cpRf(src, dest);
110134
}
@@ -113,7 +137,10 @@ export async function copyStaticFiles() {
113137
// replace angular.io to angular.jp in sitemap.xml
114138
export async function modifySitemap() {
115139
await $`chmod -R +w ${resolve(outDir, 'dist/bin/aio/build')}`;
116-
const sitemapPath = resolve(outDir, 'dist/bin/aio/build/generated/sitemap.xml');
140+
const sitemapPath = resolve(
141+
outDir,
142+
'dist/bin/aio/build/generated/sitemap.xml'
143+
);
117144
await sed(sitemapPath, 'angular.io', 'angular.jp');
118145
}
119146

tools/watch.mjs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#!/usr/bin/env zx
22

33
import { argv, chalk } from 'zx';
4-
import { applyPatches, copyLocalizedFiles, resetBuildDir, watchAIO, watchLocalizedFiles } from './lib/common.mjs';
4+
import {
5+
applyPatches,
6+
resetBuildDir,
7+
serveAdev,
8+
watchLocalizedFiles,
9+
} from './lib/common.mjs';
510

611
try {
712
const { init = false } = argv;
@@ -19,11 +24,17 @@ try {
1924

2025
async function setup({ init }) {
2126
console.log('');
22-
console.log(chalk.white('変更監視の対象は、aio-ja 内のファイル と build/aio 内のソースコードです。'));
27+
console.log(chalk.white('変更監視の対象は、adev-ja 内のファイルです'));
2328
if (init) {
24-
console.log(chalk.yellow('build ディレクトリを初期化し、キャッシュを破棄します。'));
29+
console.log(
30+
chalk.yellow('build ディレクトリを初期化し、キャッシュを破棄します。')
31+
);
2532
} else {
26-
console.log(chalk.white('build ディレクトリを初期化するには --init オプションを指定してください。'));
33+
console.log(
34+
chalk.white(
35+
'build ディレクトリを初期化するには --init オプションを指定してください。'
36+
)
37+
);
2738
}
2839
console.log('');
2940
await resetBuildDir({ init });
@@ -32,8 +43,8 @@ async function setup({ init }) {
3243
async function preWatch({ init }) {
3344
if (init) {
3445
// copy translated files
35-
console.log(chalk.cyan('Copy localized files...'));
36-
await copyLocalizedFiles();
46+
// console.log(chalk.cyan('Copy localized files...'));
47+
// await copyLocalizedFiles();
3748

3849
// apply patches
3950
console.log(chalk.cyan('Apply patches...'));
@@ -42,12 +53,13 @@ async function preWatch({ init }) {
4253
}
4354

4455
async function watch() {
45-
const ctrl = new AbortController();
46-
await watchLocalizedFiles(ctrl.signal);
47-
try {
48-
await watchAIO();
49-
} finally {
50-
console.log(chalk.cyan('Abort watching...'));
51-
ctrl.abort();
52-
}
56+
let adevProcess = serveAdev();
57+
console.log(chalk.cyan('Start watching adev-ja files...'));
58+
watchLocalizedFiles(async () => {
59+
if (adevProcess != null) {
60+
await adevProcess.cancel();
61+
}
62+
console.log(chalk.cyan('Restarting adev...'));
63+
adevProcess = serveAdev();
64+
});
5365
}

0 commit comments

Comments
 (0)