-
Notifications
You must be signed in to change notification settings - Fork 4
/
sync-fs.js
149 lines (141 loc) · 4.46 KB
/
sync-fs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const assert = require( 'assert' );
const { createHash } = require( 'crypto' );
const { readFile, mkdir, access, copyFile, unlink, writeFile } = require( 'fs/promises' );
const { resolve, join } = require( 'path' );
const { bold } = require( 'chalk' );
const { glob } = require( 'glob' );
const { memoize, partition, isString } = require( 'lodash' );
const { tryReadFile } = require( './utils' );
const checksum = async file => createHash( 'md5' )
.update( ( await readFile( file, 'utf-8' ) ).replace( /\r?\n/g, '\n' ), 'utf-8' )
.digest( 'hex' );
const cacheFile = resolve( __dirname, '../.sync-proto-cache' );
const readCache = memoize( async () => {
try {
const cacheContent = ( await tryReadFile( cacheFile, 'utf-8' ) ) ?? '';
return cacheContent
.split( /\r?\n/ )
.filter( v => v.trim() )
.reduce( ( acc, line ) => {
const parts = line.split( '::' ).map( p => p.trim() );
if( parts.length === 0 ){
return acc;
}
assert( parts.length === 2 );
const [ file, hash ] = parts;
acc[file] = hash;
return acc;
}, {} );
} catch( e ){
return {};
}
} );
const protoFs = memoize( async ( proto, handlers ) => {
const filesDirs = ( await glob( '**', { cwd: proto, ignore: [ '**/node_modules/**' ], mark: true, dot: true } ) )
.filter( fd => !( handlers.some( h => h.handleFile?.( fd ) ?? false ) ) );
const [ dirs, files ] = partition( filesDirs, p => p.endsWith( '/' ) );
return { dirs, files };
} );
const getChangedFiles = memoize( async ( proto, handlers ) => {
const [ cacheContent, { files } ] = await Promise.all( [
readCache(),
protoFs( proto, handlers ),
] );
const changedFiles = {};
await Promise.all( files.map( async file => {
const hash = await checksum( resolve( proto, file ) );
if( !cacheContent[file] || cacheContent[file] !== hash ){
changedFiles[file] = hash;
}
} ) );
Object.keys( cacheContent ).forEach( k => {
if( !files.includes( k ) ){
changedFiles[k] = undefined;
}
} );
return changedFiles;
} );
/**
* @param {boolean} checkOnly
* @returns {import('./utils').ProtoHandler}
*/
module.exports.syncFs = checkOnly => {
const conflicting = [];
return {
name: 'syncFs',
run: async ( proto, { path: projectPath }, _projects, handlers ) => {
try {
const { dirs, files } = await protoFs( proto, handlers );
for( const dir of dirs ){
await mkdir( resolve( projectPath, dir ), { recursive: true } );
}
const changedFiles = await getChangedFiles( proto, handlers );
if( checkOnly ){
const changedFilesNames = Object.keys( changedFiles );
assert.equal( changedFilesNames.length, 0, `Some files has changed compared to prototype. ${changedFilesNames.join( ' ' )}` );
} else {
await Promise.all( Object.entries( changedFiles ).map( async ( [ file, protoSum ] ) => {
const source = resolve( proto, file );
const dest = resolve( projectPath, file );
if( protoSum ){
try{
await access( dest );
const prevSum = ( await readCache() )[file];
if( ( prevSum ?? protoSum ) !== await checksum( dest ) ){
const projectFile = join( projectPath, file );
conflicting.push( projectFile );
}
} catch( err ){
if ( err.code !== 'ENOENT' ) {
throw err;
}
}
await copyFile( source, dest );
} else {
try{
await unlink( dest );
// eslint-disable-next-line no-empty -- No error
} catch( e ){}
}
} ) );
}
await Promise.all( files.map( async file => {
if( file in changedFiles ){
return;
}
const absFile = resolve( projectPath, file );
try{
await access( absFile );
} catch( e ){
if( checkOnly ){
assert.fail( `Missing ${absFile}` );
}
await copyFile( resolve( proto, file ), absFile );
}
} ) );
} catch( e ){
throw new Error( `Error in package ${projectPath}`, { cause: e } );
}
},
tearDown: async ( proto, _projects, handlers ) => {
if( checkOnly ){
return;
}
conflicting.forEach( c => {
console.error( `File ${bold( c )} has been changed compared to prototype. Please review git changes.` );
} );
const [ cache, changed ] = await Promise.all( [
readCache(),
getChangedFiles( proto, handlers ),
] );
const newCache = {
...cache,
...changed,
};
await writeFile( cacheFile, Object.entries( newCache )
.filter( ( [ , v ] ) => isString( v ) )
.map( entry => entry.join( ' :: ' ) )
.join( '\n' ) );
},
};
};