Skip to content

Commit

Permalink
* (bluefox) Added log outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Sep 21, 2024
1 parent 0435e51 commit b7e3920
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ node node_modules/@iobroker/build-tools/convertI18n.js path/to/i18n
### **WORK IN PROGRESS**
-->
## Changelog
### **WORK IN PROGRESS**
* (bluefox) Added log outputs

### 1.0.4 (2024-09-20)
* (bluefox) Added the build support for vite and typescript

Expand Down
37 changes: 21 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export function deleteFoldersRecursive(
try {
rmdirSync(curPath);
} catch (e) {
console.warn(`Cannot delete "${curPath}: ${e}`);
console.warn(`[${new Date().toISOString()}] Cannot delete "${curPath}: ${e}`);
}
} else {
try {
unlinkSync(curPath);
} catch (e) {
console.warn(`Cannot delete "${curPath}: ${e}`);
console.warn(`[${new Date().toISOString()}] Cannot delete "${curPath}: ${e}`);
}
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ export function copyFiles(
if (!existsSync(folder)) {
mkdirSync(folder, { recursive: true });
}
console.log(`Copy "${files[f].base}/${files[f].name}" to "${destName}"`);
console.log(`[${new Date().toISOString()}] Copy "${files[f].base}/${files[f].name}" to "${destName}"`);
if (options) {
let data = readFileSync(files[f].base ? `${files[f].base}/${files[f].name}` : files[f].name).toString('utf8');
if (options.replace) {
Expand Down Expand Up @@ -211,7 +211,7 @@ export function npmInstall(
if (code && code !== 1) {
reject(`Cannot install: ${code}`);
} else {
console.log(`"${cmd} in ${cwd} finished.`);
console.log(`[${new Date().toISOString()}] "${cmd} in ${cwd} finished.`);
// command succeeded
resolve();
}
Expand Down Expand Up @@ -257,15 +257,15 @@ export function tsc(
}

if (!existsSync(script)) {
console.error(`Cannot find execution file: ${script}`);
console.error(`[${new Date().toISOString()}] Cannot find execution file: ${script}`);
reject(`Cannot find execution file: ${script}`);
} else {
const child: ChildProcess = fork(script, [], cpOptions);
child?.stdout?.on('data', data => console.log(data.toString()));
child?.stderr?.on('data', data => console.log(data.toString()));
child?.stdout?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));
child?.stderr?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));

child.on('close', code => {
console.log(`child process exited with code ${code}`);
console.log(`[${new Date().toISOString()}] child process exited with code ${code}`);
code ? reject(`Exit code: ${code}`) : resolve();
});
}
Expand Down Expand Up @@ -305,9 +305,12 @@ export function buildReact(
const version = JSON.parse(readFileSync(`${rootDir}/package.json`).toString('utf8')).version;
const data = JSON.parse(readFileSync(`${src}/package.json`).toString('utf8'));

data.version = version;
if (data.version !== version) {
console.log(`[${new Date().toISOString()}] updated version in "${src}/package.json to "${version}"`);
data.version = version;

writeFileSync(`${src}/package.json`, JSON.stringify(data, null, 4));
writeFileSync(`${src}/package.json`, JSON.stringify(data, null, 4));
}
}
const reactPromise: Promise<void> = new Promise((resolve, reject) => {
const cpOptions: CommonSpawnOptions = {
Expand Down Expand Up @@ -344,7 +347,7 @@ export function buildReact(
}
}
}
}else {
} else {
script = `${src}/node_modules/react-scripts/scripts/build.js`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/react-scripts/scripts/build.js`;
Expand All @@ -359,21 +362,23 @@ export function buildReact(
}

if (!existsSync(script)) {
console.error(`Cannot find execution file: ${script}`);
console.error(`[${new Date().toISOString()}] Cannot find execution file: ${script}`);
reject(`Cannot find execution file: ${script}`);
} else {
let child: ChildProcess;
if (options?.ramSize || options?.exec) {
const cmd = `node ${script}${options.ramSize ? ` --max-old-space-size=${options.ramSize}` : ''} build`;
console.log(`[${new Date().toISOString()}] Execute: "${cmd}" ${JSON.stringify(cpOptions)}`);
child = exec(cmd, cpOptions as ExecOptions);
} else {
console.log(`[${new Date().toISOString()}] fork: "${script} build" ${JSON.stringify(cpOptions)}`);
child = fork(script, ['build'], cpOptions);
}
child?.stdout?.on('data', data => console.log(data.toString()));
child?.stderr?.on('data', data => console.log(data.toString()));
child?.stdout?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));
child?.stderr?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));

child.on('close', code => {
console.log(`child process exited with code ${code}`);
console.log(`[${new Date().toISOString()}] child process exited with code ${code}`);
code ? reject(`Exit code: ${code}`) : resolve();
});
}
Expand All @@ -400,7 +405,7 @@ export function buildCraco(
ramSize?: number,
},
): Promise<void> {
console.warn('buildCraco deprecated: Please use buildReact with craco option');
console.warn(`[${new Date().toISOString()}] buildCraco deprecated: Please use buildReact with craco option`);
return buildReact(src, { craco: true, ...options });
}

Expand Down

0 comments on commit b7e3920

Please sign in to comment.