@reflex-stack/tsp 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # TSP
1
+ # TypeScript Package (TSP)
2
2
 
3
3
  **TypeScript Package** (tsp), scaffolds and build **Typescript** sources to **EcmaScript modules** and publish them as modular packages to **NPM** or **JSR**.
4
4
 
package/package.json CHANGED
@@ -1,23 +1,34 @@
1
1
  {
2
- "name": "@reflex-stack/tsp",
3
- "version": "0.1.0",
4
- "type": "module",
5
- "bin": {
6
- "tsp": "./src/cli.js"
7
- },
8
- "scripts": {
9
- "publish": "node ./src/cli.js publish"
10
- },
11
- "author": "Alexis Bouhet",
12
- "license": "MIT",
13
- "description": "",
14
- "dependencies": {
15
- "@types/node": "^22.10.2",
16
- "@zouloux/cli": "^0.2.8",
17
- "@zouloux/files": "^3.0.4",
18
- "brotli-size": "^4.0.0",
19
- "stach": "^2.0.1",
20
- "terser": "^5.37.0",
21
- "typescript": "^5.7.2"
22
- }
2
+ "name": "@reflex-stack/tsp",
3
+ "version": "0.1.2",
4
+ "type": "module",
5
+ "bin": {
6
+ "tsp": "./src/cli.js"
7
+ },
8
+ "exports": {
9
+ ".": "./src/cli.js",
10
+ "./build": "./src/commands/build.js",
11
+ "./init": "./src/commands/init.js",
12
+ "./size-report": "./src/commands/size-report.js",
13
+ "./tests": "./src/tests.js"
14
+ },
15
+ "scripts": {
16
+ "publish": "node ./src/cli.js publish"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/reflex-stack/tsp"
21
+ },
22
+ "author": "Alexis Bouhet",
23
+ "license": "MIT",
24
+ "description": "TSP scaffolds and build Typescript sources to EcmaScript modules and publish them as modular packages to NPM",
25
+ "dependencies": {
26
+ "@types/node": "^22.10.2",
27
+ "@zouloux/cli": "^0.2.8",
28
+ "@zouloux/files": "^3.0.4",
29
+ "brotli-size": "^4.0.0",
30
+ "stach": "^2.0.1",
31
+ "terser": "^5.37.0",
32
+ "typescript": "^5.7.2"
33
+ }
23
34
  }
package/src/cli.js CHANGED
@@ -1,15 +1,12 @@
1
1
  #!/usr/bin/env sh
2
2
  ':' //# comment; exec /usr/bin/env "${RUNTIME:-node}" "$0" "$@"
3
3
 
4
-
5
4
  import { askInput, CLICommands, execAsync, nicePrint, oraTask, execSync, table, askList, newLine } from "@zouloux/cli";
6
5
  import { build, clearOutput } from "./commands/build.js";
7
6
  import { getUserPackageJson, naiveHumanFileSize, showIntroMessage } from "./utils.js";
8
7
  import { cleanSizeReports, generateJSON, generateSVGs, sizeReport } from "./commands/size-report.js";
9
8
  import { init } from "./commands/init.js";
10
9
  import { getConfig } from "./config.js";
11
- import { test } from "./commands/test.js";
12
-
13
10
 
14
11
  // ----------------------------------------------------------------------------- COMMANDS
15
12
 
@@ -142,9 +139,25 @@ commands.add("test", async (args, flags, commandName) => {
142
139
  const userPackage = getUserPackageJson()
143
140
  const config = getConfig(userPackage)
144
141
 
145
- await test( config )
142
+ nicePrint(`Starting test sequence`)
143
+ newLine()
144
+ for ( const testFile of config["test-files"] ) {
145
+ const command = `${config.runtime} ${config.tests}/${testFile}`
146
+ nicePrint(`{d}$ ${command}`)
147
+ try {
148
+ await execAsync(command, 3)
149
+ }
150
+ catch ( error ) {
151
+ nicePrint(`{b/r}Test ${testFile} failed`, { code: 2 })
152
+ }
153
+ newLine()
154
+ }
155
+ await oraTask(`All tests passed`, (t) => {t.success()})
146
156
  })
147
157
 
158
+ /**
159
+ * PUBLISH COMMAND
160
+ */
148
161
  commands.add("publish", async (args, flags, commandName) => {
149
162
  // Check NPM connected user
150
163
  await oraTask({text: `Connecting to npm`}, async task => {
@@ -215,6 +228,10 @@ commands.add("publish", async (args, flags, commandName) => {
215
228
  }
216
229
  })
217
230
 
231
+
232
+ /**
233
+ * START
234
+ */
218
235
  commands.start(function (commandName) {
219
236
  if ( commandName )
220
237
  return
@@ -67,6 +67,9 @@ const tsconfigTemplate = `{
67
67
  // Compilation level target
68
68
  // If using a bundler in your project it should be the previous or current year
69
69
  "target" : "{{esLevel}}",
70
+ // Allow types to be resolved in tests
71
+ "checkJs": true,
72
+ "allowJs": true,
70
73
  // TS libs used, include needed lib.
71
74
  // It should contain the target and "DOM" if it runs in the browser
72
75
  // https://www.typescriptlang.org/tsconfig/#lib
@@ -77,7 +80,7 @@ const tsconfigTemplate = `{
77
80
  "allowImportingTsExtensions": false,
78
81
  // Compiler config
79
82
  "strict": {{tsStrict}},
80
- "allowJs": false,
83
+ "checkJs": true,
81
84
  "useDefineForClassFields" : true,
82
85
  "allowSyntheticDefaultImports": true,
83
86
  // ---
@@ -90,9 +93,17 @@ const tsconfigTemplate = `{
90
93
  // - declaration
91
94
  // - noEmitOnError
92
95
  // - pretty
93
- }
94
- }
95
- `
96
+ },
97
+ // Compile only from src, avoid looping in dist
98
+ "exclude": ["node_modules", "dist"],
99
+ "include": ["src/**/*"],
100
+ }`
101
+
102
+ const tsconfigTestTemplate = `{
103
+ "extends": "../tsconfig.json",
104
+ "exclude": ["../node_modules", "../dist", "../src"],
105
+ "include": ["./**/*"]
106
+ }`
96
107
 
97
108
  const gitIgnoreTemplate = `.DS_Store
98
109
  .idea
@@ -127,9 +138,37 @@ export function subRandomFunction () {
127
138
  }
128
139
  `
129
140
 
130
- const testFile = `// Write your test file here
131
- console.log("No test implemented yet")
132
- process.exit(0)
141
+ const testFile = `// If you have no tests, uncomment this
142
+ // console.log("No test implemented.")
143
+ // process.exit(0)
144
+
145
+ // Import your code from dist directory, tests are not built on purpose
146
+ import { randomFunction } from "../dist/index.js"
147
+ import { subRandomFunction } from "../dist/submodule/index.js"
148
+ // Import small testing lib from tsp
149
+ import { describe, it, expect, startTest } from "@reflex-stack/tsp/tests"
150
+
151
+ const endTest = startTest()
152
+
153
+ describe("Main module", () => {
154
+ it("Should call random", () => {
155
+ const rootResult = randomFunction()
156
+ expect(rootResult).toBe(5)
157
+ })
158
+ })
159
+
160
+ describe("Sub module", () => {
161
+ it("Should call sub random", () => {
162
+ const rootResult = subRandomFunction()
163
+ expect(rootResult).toBe(60)
164
+ })
165
+ // Test error example
166
+ // it("Should fail", () => {
167
+ // expect(5).toBe(12)
168
+ // })
169
+ })
170
+
171
+ endTest()
133
172
  `
134
173
 
135
174
  export async function init () {
@@ -227,8 +266,9 @@ export async function init () {
227
266
  // Generate src files
228
267
  writeFileSync(join(config.src, "index.ts"), Stach(rootIndexTs, options))
229
268
  writeFileSync(join(config.src, "submodule", "index.ts"), Stach(subModuleIndexTs, options))
230
- // Generate test file
269
+ // Generate tests file
231
270
  writeFileSync(join(config.tests, "test.js"), Stach(testFile, options))
271
+ writeFileSync(join(config.tests, "tsconfig.json"), Stach(tsconfigTestTemplate, options))
232
272
  // Install dependencies
233
273
  await oraTask("Installing dependencies", async () => {
234
274
  await execAsync(`npm i typescript terser`, false, { cwd: config.cwd })
package/src/tests.js ADDED
@@ -0,0 +1,62 @@
1
+
2
+
3
+ let total = 0
4
+ let success = 0
5
+ let failed = 0
6
+
7
+ export function startTest () {
8
+ total = 0
9
+ success = 0
10
+ failed = 0
11
+ return () => {
12
+ console.log(`\n${success} / ${total} succeeded with ${failed} error${failed > 1 ? 's' : ''}.`)
13
+ process.exit( failed )
14
+ }
15
+ }
16
+
17
+
18
+
19
+ export const describe = ( name, fn ) => {
20
+ console.log(`${ name }`)
21
+ fn()
22
+ }
23
+
24
+ export const it = ( name, fn ) => {
25
+ ++total
26
+ try {
27
+ fn()
28
+ console.log(` - ${ name }`)
29
+ ++success
30
+ } catch ( e ) {
31
+ ++failed
32
+ console.log(`❌ ${ name }`)
33
+ console.error( e )
34
+ }
35
+ }
36
+
37
+ export const expect = ( actual ) => ({
38
+ toBe: ( expected ) => {
39
+ if ( actual !== expected )
40
+ throw new Error(`Expected ${ expected } but got ${ actual }`)
41
+ },
42
+ toEqual: ( expected ) => {
43
+ if ( JSON.stringify( actual ) !== JSON.stringify( expected ) )
44
+ throw new Error(`Expected ${ JSON.stringify( expected ) } but got ${ JSON.stringify( actual ) }`)
45
+ },
46
+ toBeTruthy: () => {
47
+ if ( !actual )
48
+ throw new Error(`Expected truthy but got ${ actual }`)
49
+ },
50
+ toBeFalsy: () => {
51
+ if ( actual )
52
+ throw new Error(`Expected falsy but got ${ actual }`)
53
+ },
54
+ toThrow: () => {
55
+ try {
56
+ actual()
57
+ throw new Error( 'Expected function to throw' )
58
+ } catch ( e ) {
59
+ return true
60
+ }
61
+ }
62
+ })
@@ -1,20 +0,0 @@
1
- import { execAsync, newLine, nicePrint, oraTask } from "@zouloux/cli";
2
-
3
-
4
- export async function test ( config ) {
5
- nicePrint(`Starting test sequence`)
6
- newLine()
7
- for ( const testFile of config["test-files"] ) {
8
- const command = `${config.runtime} ${config.tests}/${testFile}`
9
- nicePrint(`{d}$ ${command}`)
10
- try {
11
- await execAsync(command, 3)
12
- }
13
- catch ( error ) {
14
- console.error(error)
15
- nicePrint(`{b/g}Test ${testFile} failed`, { code: error.code })
16
- }
17
- newLine()
18
- }
19
- await oraTask(`All tests passed`, (t) => {t.success()})
20
- }
File without changes
File without changes