11import { watch } from 'chokidar' ;
22import { resolve } from 'node:path' ;
3+ import { execa } from 'execa' ;
4+ // TODO: replace $ to execa
35import { $ , cd , chalk , glob , within } from 'zx' ;
6+ import kill from 'tree-kill' ;
47import { 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+
616const rootDir = resolve ( __dirname , '../' ) ;
7- const adevDir = resolve ( rootDir , 'adev-ja' ) ;
17+ const adevJaDir = resolve ( rootDir , 'adev-ja' ) ;
818const aiojaDir = resolve ( rootDir , 'aio-ja' ) ;
919const 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
4669export 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
7399export async function applyPatches ( ) {
@@ -100,11 +126,9 @@ export async function copyRobots() {
100126// Copy static files into build output directory
101127export 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
114138export 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
0 commit comments