Skip to content

Commit 1bf8711

Browse files
authored
Base of Monorepo (#685)
1 parent 9d1637b commit 1bf8711

File tree

248 files changed

+16564
-501
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

248 files changed

+16564
-501
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ plugins
1616
.DS_Store
1717
/dist
1818
ignore
19-
/lib/**/index.js
19+
/lib/**/index.js
20+
.ignore
21+
.tmp
22+
.coverage
23+
pnpm-lock.yaml

bin/add.mjs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url';
44
import ora from 'ora';
55
import addLicense from './addLicense.mjs';
66

7+
// eslint-disable-next-line @typescript-eslint/naming-convention
78
const __dirname = path.dirname(fileURLToPath(import.meta.url));
89

910
const folderStructure = (lib) => [
@@ -273,7 +274,7 @@ test('log', () => {
273274
{
274275
name: 'README.md',
275276
type: 'file',
276-
content: `# @akarui/${lib}
277+
content: `# @aoijs/${lib}
277278
278279
A Extension for Aoi.js
279280
`,
@@ -490,6 +491,44 @@ See the License for the specific language governing permissions and
490491
limitations under the License.
491492
`,
492493
},
494+
{
495+
name: '.swcrc',
496+
type: 'file',
497+
content: `{
498+
"$schema": "https://swc.rs/schema.json",
499+
"module": {
500+
"type": "es6",
501+
"resolveFully": true
502+
},
503+
"jsc": {
504+
"parser": {
505+
"syntax": "typescript",
506+
"jsx": false,
507+
"dynamicImport": true,
508+
"privateMethod": true,
509+
"functionBind": true,
510+
"exportDefaultFrom": true,
511+
"exportNamespaceFrom": true,
512+
"decorators": true,
513+
"decoratorsBeforeExport": true,
514+
"topLevelAwait": true,
515+
"importMeta": true
516+
},
517+
"target": "esnext",
518+
"baseUrl": "./",
519+
"paths": {
520+
"@${lib}/*": ["./src/*"],
521+
"@aoirepo/*": ["../../*"]
522+
},
523+
"loose": false,
524+
"externalHelpers": false,
525+
// Requires v1.2.50 or upper and requires target to be es2016 or upper.
526+
"keepClassNames": true,
527+
"preserveAllComments": true
528+
},
529+
"minify": false
530+
}`,
531+
},
493532
];
494533

495534
/**

bin/docs.mjs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
const docs = async ({ library }) => {
1+
import path from 'node:path';
2+
import { exec } from 'node:child_process';
3+
import { promisify } from 'node:util';
4+
5+
const execAsync = promisify(exec);
6+
7+
/**
8+
* Generate the documentation for the given library
9+
* @param {object} param0 - The library to generate the documentation for
10+
* @param {string} param0.library - The library to generate the documentation for
11+
*/
12+
const docs = async ({ library }) => {
213
console.log(`Generating documentation for ${library}`);
14+
15+
let libPath = path.resolve(process.cwd(), 'tools');
16+
17+
if (library === 'aoi.js') {
18+
libPath = path.resolve(libPath, 'aoijs-tooling', 'getFunctionData.mjs');
19+
}
20+
21+
const savedJSONPath = path.resolve(process.cwd(), 'lib', library, 'docs', 'data.json');
22+
23+
const { stdout, stderr } = await execAsync(`node --import tsx ${libPath}`, {
24+
cwd: path.resolve(process.cwd(), 'lib', library),
25+
});
326
};
427

5-
export default docs;
28+
export default docs;

bin/genTest.mjs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import path from 'node:path';
2+
import { spawn } from 'node:child_process';
3+
import { pathToFileURL } from 'node:url';
4+
import chalk from 'chalk';
5+
/**
6+
* generate all tests for the given library at the given folder
7+
* @param {object} options - The options object
8+
* @param {string} options.library - The library to test
9+
* @returns {Promise<void>}
10+
*/
11+
const genTest = async ({ library, folder }) => {
12+
const start = performance.now();
13+
console.log(`Generating tests for ${library} at ${folder }`);
14+
15+
// Resolve paths in a cross-platform way
16+
const mainFolder = path.join(process.cwd(), 'lib', library);
17+
18+
const flags = folder ? `--folder=${folder}` : '';
19+
20+
const generatorPath = library === 'aoi.js' ? ['aoijs-tooling', 'testGenerator.mjs'] : ['aoijs-tooling', 'testGenerator.mjs'];
21+
22+
const runnerPath = path.join(
23+
process.cwd(),
24+
'tools',
25+
...generatorPath,
26+
);
27+
// "glob -c \"tsx --test\" \"./src/**/*.test.ts\""
28+
const spwn = spawn(
29+
// `npx glob -c "tsx --test-reporter="${reporterPath.toString()}" --test" "./src/**/*.test.ts"`,
30+
`node --import tsx "${runnerPath.toString()}" -- ${flags}`,
31+
[],
32+
{
33+
stdio: 'inherit',
34+
// Set cwd to the project root
35+
cwd: `${mainFolder}`,
36+
shell: true,
37+
},
38+
);
39+
40+
spwn.on('exit', (code) => {
41+
if (code !== 0) {
42+
console.error(`Failed to test ${library}`);
43+
process.exit(1);
44+
}
45+
});
46+
47+
spwn.on('error', (error) => {
48+
console.error(error);
49+
process.exit(1);
50+
});
51+
52+
spwn.on('close', () => {
53+
console.log(`Generated tests in ${folder} for ${library}`);
54+
console.log(
55+
chalk.gray(
56+
`Duration: ${((performance.now() - start) / 1000).toFixed(2)}s`,
57+
),
58+
);
59+
});
60+
};
61+
62+
export default genTest;

bin/index.mjs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import build from './build.mjs';
66
import publish from './publish.mjs';
77
import version from './version.mjs';
88
import docs from './docs.mjs';
9-
import pkg from './package.json' assert { type: 'json' };
9+
import pkg from './package.json' with { type: 'json' };
1010
import chalk from 'chalk';
1111
import add from './add.mjs';
1212
import addLicense from './addLicense.mjs';
1313
import run from './run.mjs';
14+
import genTest from './genTest.mjs';
15+
import { init } from './init.mjs';
1416

1517
program
1618
.command('test')
@@ -63,6 +65,19 @@ program
6365
.requiredOption('-f, --file <file>', 'the file to run')
6466
.action(run);
6567

68+
program
69+
.command('genTest')
70+
.description('generate all tests for the given library')
71+
.requiredOption('-l, --library <library>', 'the library to test')
72+
.requiredOption('-f, --folder <folder>', 'the folder to test')
73+
.action(genTest);
74+
75+
program
76+
.command('init')
77+
.description('initialize the given library')
78+
.requiredOption('-l, --library <library>', 'the library to initialize')
79+
.action(init);
80+
6681
program
6782
.name(pkg.name)
6883
.version(pkg.version)

bin/init.mjs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { exec } from 'node:child_process';
2+
import path from 'node:path';
3+
import { promisify } from 'node:util';
4+
5+
const execAsync = promisify(exec);
6+
7+
async function initAoijs() {
8+
// first install all the deps in root
9+
// then build structures folder for aoi.js
10+
// cd into lib/aoi.js
11+
// run pnpm install
12+
// cd back to root
13+
// run aoirepo build -l aoi.js
14+
15+
const root = process.cwd();
16+
const aoijs = './lib/aoi.js';
17+
18+
console.log('Installing all dependencies in root...');
19+
let res = await execAsync('pnpm install -w', {
20+
cwd: root,
21+
});
22+
23+
if (res.stderr) {
24+
console.error(res.stderr);
25+
return;
26+
}
27+
28+
console.log('Patching tsc with ts-patch...');
29+
res = await execAsync('npx ts-patch install', {
30+
cwd: root,
31+
});
32+
33+
if (res.stderr) {
34+
console.error(res.stderr);
35+
return;
36+
}
37+
38+
console.log('Building structures folder for aoi.js...');
39+
res = await execAsync('aoirepo build -l structures', {
40+
cwd: root,
41+
});
42+
43+
if (res.stderr) {
44+
console.error(res.stderr);
45+
return;
46+
}
47+
48+
console.log('Installing all dependencies in aoi.js...');
49+
50+
res = await execAsync('pnpm install', {
51+
cwd: path.resolve(root, aoijs),
52+
});
53+
54+
if (res.stderr) {
55+
console.error(res.stderr);
56+
return;
57+
}
58+
59+
console.log('Building aoi.js...');
60+
res = await execAsync('aoirepo build -l aoi.js', {
61+
cwd: root,
62+
});
63+
64+
if (res.stderr) {
65+
console.error(res.stderr);
66+
return;
67+
}
68+
69+
console.log('Done!');
70+
}
71+
72+
export async function init({ library }) {
73+
if (library === 'aoi.js') {
74+
await initAoijs();
75+
}
76+
77+
process.exit(0);
78+
}

bin/test.mjs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
11
import path from 'node:path';
2-
import { spawn } from 'node:child_process';
3-
import { pathToFileURL } from 'node:url';
2+
import { exec, spawn } from 'node:child_process';
43
import chalk from 'chalk';
4+
import { existsSync } from 'node:fs';
5+
import { rm } from 'node:fs/promises';
6+
import { promisify } from 'node:util';
7+
8+
const execAsync = promisify(exec);
9+
510
/**
611
* Run all tests for the given library
712
* @param {object} options - The options object
813
* @param {string} options.library - The library to test
14+
* @param {string} [options.folder] - The folder to test
915
* @returns {Promise<void>}
1016
*/
11-
const test = async ({ library, folder, reporter }) => {
17+
const test = async ({ library, folder }) => {
1218
const start = performance.now();
1319
console.log(`Running tests for ${library}`);
1420

1521
// Resolve paths in a cross-platform way
1622
const mainFolder = path.join(process.cwd(), 'lib', library);
17-
const reporterPath = pathToFileURL(
18-
path.join(process.cwd(), 'tools', 'testing', 'testReporter.mjs'),
19-
);
2023
const flags = folder ? `--folder=${folder}` : '';
2124
const runnerPath = path.join(
2225
process.cwd(),
2326
'tools',
2427
'testing',
2528
'testRunner.mjs',
2629
);
27-
// "glob -c \"tsx --test\" \"./src/**/*.test.ts\""
30+
31+
if (!folder?.endsWith('.test.ts')) {
32+
if (existsSync(mainFolder + '/.tmp')) {
33+
await rm(mainFolder + '/.tmp', { recursive: true });
34+
}
35+
36+
const build = await execAsync(
37+
`npx swc ./src -d ${mainFolder}/.tmp`,
38+
{
39+
cwd: mainFolder,
40+
shell: true,
41+
},
42+
);
43+
44+
45+
if (build.stderr) {
46+
console.error(build.stderr);
47+
process.exit(1);
48+
}
49+
}
50+
2851
const spwn = spawn(
29-
// `npx glob -c "tsx --test-reporter="${reporterPath.toString()}" --test" "./src/**/*.test.ts"`,
30-
`node --import tsx "${runnerPath.toString()}" -- ${flags}`,
52+
// `npx glob -c "tsx --test --experimental-test-coverage --test-reporter=lcov --test-reporter=spec" "./src/**/*.test.ts"`,
53+
(folder?.endsWith('.test.ts'))
54+
? `node --import tsx "${runnerPath.toString()}" -- ${flags}` :
55+
`node "${runnerPath.toString()}" -- ${flags}`,
3156
[],
3257
{
3358
stdio: 'inherit',
3459
// Set cwd to the project root
35-
cwd: `${mainFolder}`,
60+
cwd: (!folder?.endsWith('.test.ts'))
61+
? `${mainFolder}/.tmp/src` : `${mainFolder}/src`,
3662
shell: true,
3763
},
3864
);
3965

40-
spwn.on('exit', (code) => {
66+
spwn.on('exit', async (code) => {
4167
if (code !== 0) {
4268
console.error(`Failed to test ${library}`);
69+
70+
if ((!folder?.endsWith('.test.ts'))) await rm(mainFolder + '/.tmp', { recursive: true });
4371
process.exit(1);
4472
}
4573
});
@@ -49,13 +77,15 @@ const test = async ({ library, folder, reporter }) => {
4977
process.exit(1);
5078
});
5179

52-
spwn.on('close', () => {
80+
spwn.on('close', async () => {
5381
console.log(`Tested ${library}`);
5482
console.log(
5583
chalk.gray(
5684
`Duration: ${((performance.now() - start) / 1000).toFixed(2)}s`,
5785
),
5886
);
87+
88+
if ((!folder?.endsWith('.test.ts'))) await rm(mainFolder + '/.tmp', { recursive: true });
5989
});
6090
};
6191

0 commit comments

Comments
 (0)