Skip to content

Commit

Permalink
Addedd description of all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Oct 3, 2024
1 parent 176b3ad commit 193f195
Show file tree
Hide file tree
Showing 6 changed files with 518 additions and 71 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: 20.x
- run: npm i
- run: npm ci
- run: npm run build
- run: npm run test

# Deploys the final package to NPM
deploy:
Expand Down Expand Up @@ -86,7 +87,7 @@ jobs:
echo "::set-output name=BODY::$BODY"
- name: Install Dependencies
run: npm install -f
run: npm ci

- name: Create a clean build
run: npm run build
Expand Down
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,158 @@ And use in `package.json` `scripts`:
}
```

## Tools

### deleteFoldersRecursive

Delete all files and folders in the given directory.
The path could be relative or absolute.
This function operates synchronously.
The target folder itself will not be deleted.

Usage: `deleteFoldersRecursive(__dirname + '/src');`

### copyFolderRecursiveSync

The `copyFolderRecursiveSync` function is used to copy all files and subdirectories from a source directory to a destination directory.
This function operates synchronously, meaning it will block the execution of the program until the copying process is complete.
It is useful for tasks that require a complete and immediate copy of a directory structure, such as backup operations or preparing files for deployment.

The target directory will be created if not exists.
The path could be relative or absolute.

Usage: `copyFolderRecursiveSync(__dirname + '/src/build', __dirname + '/dist');`

### readDirRecursive

The readDirRecursive function is used to read all files and subdirectories within a given directory recursively.
This function is useful for tasks that require processing or listing all files within a directory tree,
such as file indexing, searching, or batch processing operations.

The function operates synchronously.

Usage:

```js
const files = readDirRecursive(__dirname + '/src');
console.log(files);
```

Parameters:

- `dir`: The directory path to read. The path can be relative or absolute.
Returns:
- An array of file paths representing all files found within the specified directory and its subdirectories.

### copyFiles

The copyFiles function is used to copy specific files from a source directory to a destination directory. This function is useful for tasks that require selective copying of files based on certain patterns or criteria, such as preparing files for deployment or creating backups of specific files.

Usage:

```js
const files = copyFiles(['src/build/**/*.js', '!src/build/**/*node_modules*.js'], __dirname + '/dist');
console.log(files);
```

Parameters:

- `src`: Array of the patterns. Negative pattern starts with '!'. You can use the [glob](https://code.visualstudio.com/docs/editor/glob-patterns) patterns.
- `dest`: The destination directory path where files will be copied to. The path can be relative or absolute.

### npmInstall
The `npmInstall` function is used to install npm packages for a given project. This function is useful for automating the setup process of a project by programmatically running `npm install` to ensure all dependencies are installed.

The function returns promise and it is not synchron.

Usage:
```js
npmInstall(__dirname + '/src')
.catch(e => console.error('Cannot install packages: ' + e.toString()));
```

### buildReact
The buildReact function is used to build a React application.
This function is useful for automating the build process of a React project,
ensuring that all necessary steps are taken to compile and bundle the application for production.

Usage for craco:
```js
buildReact(__dirname + '/src', { craco: true, rootDir: __dirname, ramSize: 7000 })
.catch(e => console.error('Cannot install packages: ' + e.toString()));
```

Usage for vite:
```js
buildReact(__dirname + '/src', { vite: true, rootDir: __dirname })
.catch(e => console.error('Cannot install packages: ' + e.toString()));
```

Usage for application with React scripts:
```js
buildReact(__dirname + '/src', { rootDir: __dirname })
.catch(e => console.error('Cannot install packages: ' + e.toString()));
```

Possible options:
- `rootDir` - sometimes the craco, vite, react-scripts are installed in the main directory and not in the src directory. This parameter says to function to look in the root directory for the executable script too.
- `ramSize` - adds the parameter `--max-old-space-size=X` (in MB) to the node parameter to increase the memory for build. `vis-2` or `admin` require it.
- `tsc` - Executes `tsc` command in the target directory before build.
- `craco` - Used `craco` for build
- `vite` - Used `vite` for build

### ignoreWidgetFiles
Create a list of glob patterns for files that must be ignored by coping of the widget files. Used together with `copyWidgetFiles`.

### copyWidgetsFiles
Create a list of glob patterns for files, that must be copied for widgets.

Usage:
```js
copyFiles([
__dirname + '/src/build/**/*.*',
copyWidgetsFiles(__dirname + '/src/build'),
ignoreWidgetFiles(__dirname + '/src/build')
], __dirname + '/widgets/vis-2-widgets-adapter-name');
```

### patchHtmlFile
Replace in the given HTML file the debug script with production one:
```html
<script>
const script = document.createElement('script');
window.registerSocketOnLoad = function (cb) {
window.socketLoadedHandler = cb;
};
script.onload = function () {
typeof window.socketLoadedHandler === 'function' && window.socketLoadedHandler();
};
script.onerror = function () {
console.error('Cannot load socket.io. Retry in 5 seconds');
setTimeout(function () {
window.location.reload();
}, 5000);
};
setTimeout(() => {
script.src = window.location.port === '3000' ? `${window.location.protocol}//${window.location.hostname}:8081/lib/js/socket.io.js` : './lib/js/socket.io.js';
}, 1000);
document.head.appendChild(script);
</script>
```

will be replaced with
```html
<script type="text/javascript" onerror="setTimeout(function(){window.location.reload()}, 5000)" src="./lib/js/socket.io.js"></script>
```

Usage:
`patchHtmlFile('admin/index_m.html')`

## Converting i18n structure

You can convert the old i18n structure i18n/lang/translations.json to the new structure i18n/lang.json with the following command:
Expand All @@ -44,6 +196,7 @@ node node_modules/@iobroker/build-tools/convertI18n.js path/to/i18n
-->

## Changelog

### 1.1.1 (2024-10-03)

- (@GermanBluefox) Trying to fix a build script for craco
Expand Down
39 changes: 14 additions & 25 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs, {
unlinkSync,
mkdirSync,
} from 'node:fs';
import { globSync } from 'glob';
import { type ChildProcess, exec, fork, type IOType, type CommonSpawnOptions, execFile } from 'node:child_process';
import { dirname, join } from 'node:path';

Expand All @@ -24,7 +25,7 @@ export function deleteFoldersRecursive(
if (existsSync(path)) {
const files = readdirSync(path);
for (const file of files) {
const curPath = `${path}/${file}`;
const curPath = join(path, file);
if (exceptions?.find(e => curPath.endsWith(e))) {
continue;
}
Expand Down Expand Up @@ -60,7 +61,7 @@ export function copyFolderRecursiveSync(
exclude?: string[],
): void {
const stats = existsSync(src) ? statSync(src) : null;
if (stats && stats.isDirectory()) {
if (stats?.isDirectory()) {
!fs.existsSync(dest) && fs.mkdirSync(dest);
fs.readdirSync(src).forEach(childItemName => {
copyFolderRecursiveSync(join(src, childItemName), join(dest, childItemName));
Expand All @@ -76,7 +77,7 @@ export function readDirRecursive(path: string, _list?: string[]): string[] {
if (existsSync(path)) {
const files = readdirSync(path);
files.forEach((file: string) => {
const fullPath = `${path}/${file}`;
const fullPath = join(path, file).replace(/\\/g, '/');
if (statSync(fullPath).isDirectory()) {
readDirRecursive(fullPath, _list);
} else {
Expand All @@ -99,6 +100,7 @@ export function collectFiles(patterns: string[] | string): { name: string; base:
add = false;
}
_patterns[i] = _patterns[i].replace(/\\/g, '/');

let folder = _patterns[i].split('*')[0];
if (folder[folder.length - 1] === '/') {
folder = folder.substring(0, folder.length - 1);
Expand All @@ -107,34 +109,21 @@ export function collectFiles(patterns: string[] | string): { name: string; base:
folderParts.pop();
folder = folderParts.join('/');
}
let files;
if (!folder) {
files = readdirSync('.');
} else {
files = readDirRecursive(folder);
}
// convert pattern "src-admin/build/static/js/*.js" to regex "src-admin/build/static/js/[^\.]+\.js"
if (_patterns[i].endsWith('*')) {
_patterns[i] = _patterns[i].replace(/\./g, '\\.').replace(/\*/g, '[^/]+');
} else {
_patterns[i] = `${_patterns[i].replace(/\./g, '\\.').replace(/\*/g, '[^/]+')}$`;
}
_patterns[i] = `^${_patterns[i]}`;

const regex = new RegExp(_patterns[i]);
const files: string[] = globSync(_patterns[i]);

for (let f = 0; f < files.length; f++) {
if (regex.test(files[f])) {
if (add) {
result.push({ name: files[f], base: folder });
} else {
const pos = result.findIndex(it => it.name === files[f]);
if (pos !== -1) {
result.splice(pos, 1);
}
if (add) {
result.push({ name: files[f], base: folder });
} else {
const pos = result.findIndex(it => it.name === files[f]);
if (pos !== -1) {
result.splice(pos, 1);
}
}
}
}

return result.map(it => ({ name: it.base ? it.name.substring(it.base.length + 1) : it.name, base: it.base }));
}

Expand Down
Loading

0 comments on commit 193f195

Please sign in to comment.