Skip to content

Commit bd304ae

Browse files
authored
chore: add support for prettier (#3360)
1 parent a74399e commit bd304ae

File tree

13 files changed

+856
-723
lines changed

13 files changed

+856
-723
lines changed

.github/workflows/wasm.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ jobs:
3131
- name: Run custom tester
3232
run: ./scripts/run-tests-wasm.sh
3333
env:
34-
TESTER: true
34+
TESTER: true
35+
- name: Run prettier
36+
uses: actionsx/prettier@v2
37+
working-directory: wasm
38+
run: prettier --check .

wasm/.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
pnpm-lock.yaml

wasm/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"semi": false,
5+
"arrowParens": "avoid"
6+
}

wasm/cli.d.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
export type RunConfig = {
2-
jwt: string
3-
defaultProjectID: string
4-
defaultOrganizationID: string
5-
apiUrl: string
2+
jwt: string
3+
defaultProjectID: string
4+
defaultOrganizationID: string
5+
apiUrl: string
66
}
77

88
export type RunResponse = {
9-
stdout: string
10-
stderr: string
11-
exitCode: string
9+
stdout: string
10+
stderr: string
11+
exitCode: string
1212
}
1313

1414
export type AutocompleteConfig = {
15-
jwt: string
16-
leftWords: string[]
17-
selectedWord: string
18-
rightWords: string[]
15+
jwt: string
16+
leftWords: string[]
17+
selectedWord: string
18+
rightWords: string[]
1919
}
2020

2121
export type Int = number
2222

2323
export type ConfigureOutputConfig = {
24-
width: Int
25-
color: boolean
24+
width: Int
25+
color: boolean
2626
}
2727

28-
2928
export type CLI = {
30-
run(cfg: RunConfig, args: string[]): Promise<RunResponse>
31-
complete(cfg: AutocompleteConfig): Promise<string[]>
32-
configureOutput(cfg: ConfigureOutputConfig): Promise<{}>
33-
stop(): Promise<void>
29+
run(cfg: RunConfig, args: string[]): Promise<RunResponse>
30+
complete(cfg: AutocompleteConfig): Promise<string[]>
31+
configureOutput(cfg: ConfigureOutputConfig): Promise<{}>
32+
stop(): Promise<void>
3433
}

wasm/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type {CLI, RunConfig, RunResponse } from './cli'
1+
export type { CLI, RunConfig, RunResponse } from './cli'
22
export type { Go } from './wasm_exec'
33

4-
export declare const wasmURL: URL;
4+
export declare const wasmURL: URL

wasm/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
const wasmURL = new URL('cli.wasm', import.meta.url)
22

3-
export {
4-
wasmURL,
5-
}
3+
export { wasmURL }

wasm/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"keywords": [],
1212
"author": "",
1313
"devDependencies": {
14+
"prettier": "3.0.3",
1415
"typescript": "^5.0.4",
1516
"vitest": "^0.30.1"
1617
},

wasm/src/cli.test.ts

Lines changed: 98 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,112 +2,128 @@
22
// It will load go misc files in
33
// /usr/local/go/misc/wasm
44

5-
import {describe, it, expect, afterAll, beforeAll} from 'vitest'
5+
import { describe, it, expect, afterAll, beforeAll } from 'vitest'
66

77
import '../wasm_exec_node.cjs'
88
import '../wasm_exec.cjs'
9-
import {CLI, RunConfig} from '../cli'
9+
import { CLI, RunConfig } from '../cli'
1010
import * as fs from 'fs'
11-
import {Go} from "../wasm_exec";
12-
import {loadWasmBinary} from "./utils";
11+
import { Go } from '../wasm_exec'
12+
import { loadWasmBinary } from './utils'
1313

1414
const CLI_PACKAGE = 'scw'
1515
const CLI_CALLBACK = 'cliLoaded'
1616

17-
const emptyConfig = (
18-
override?: {
19-
jwt?: string,
20-
defaultProjectID?: string,
21-
defaultOrganizationID?: string,
22-
apiUrl?: string
23-
}
24-
): RunConfig => ({
25-
jwt: override?.jwt || "",
26-
defaultProjectID: override?.defaultProjectID || "",
27-
defaultOrganizationID: override?.defaultOrganizationID || "",
28-
apiUrl: override?.apiUrl || ""
17+
const emptyConfig = (override?: {
18+
jwt?: string
19+
defaultProjectID?: string
20+
defaultOrganizationID?: string
21+
apiUrl?: string
22+
}): RunConfig => ({
23+
jwt: override?.jwt || '',
24+
defaultProjectID: override?.defaultProjectID || '',
25+
defaultOrganizationID: override?.defaultOrganizationID || '',
26+
apiUrl: override?.apiUrl || '',
2927
})
3028

3129
describe('With wasm CLI', async () => {
32-
let cli: CLI
33-
34-
beforeAll(async () => {
35-
// @ts-ignore
36-
cli = await loadWasmBinary('./cli.wasm') as CLI
37-
})
38-
39-
const run = async (expected: string | RegExp, command: string[], runCfg: RunConfig | null = null) => {
40-
if (runCfg === null) {
41-
runCfg = emptyConfig()
42-
}
43-
44-
const resp = await cli.run(runCfg, command)
45-
expect(resp.exitCode).toBe(0)
46-
expect(resp.stdout).toMatch(expected)
30+
let cli: CLI
31+
32+
beforeAll(async () => {
33+
// @ts-ignore
34+
cli = (await loadWasmBinary('./cli.wasm')) as CLI
35+
})
36+
37+
const run = async (
38+
expected: string | RegExp,
39+
command: string[],
40+
runCfg: RunConfig | null = null,
41+
) => {
42+
if (runCfg === null) {
43+
runCfg = emptyConfig()
4744
}
4845

49-
const runWithError = async (expected: string | RegExp, command: string[], runCfg: RunConfig | null = null) => {
50-
if (runCfg === null) {
51-
runCfg = emptyConfig()
52-
}
53-
const resp = await cli.run(runCfg, command)
54-
expect(resp.exitCode).toBeGreaterThan(0)
55-
expect(resp.stderr).toMatch(expected)
46+
const resp = await cli.run(runCfg, command)
47+
expect(resp.exitCode).toBe(0)
48+
expect(resp.stdout).toMatch(expected)
49+
}
50+
51+
const runWithError = async (
52+
expected: string | RegExp,
53+
command: string[],
54+
runCfg: RunConfig | null = null,
55+
) => {
56+
if (runCfg === null) {
57+
runCfg = emptyConfig()
5658
}
57-
58-
const complete = async (expected: string[], command: string[], runCfg: RunConfig | null = null) => {
59-
if (runCfg === null) {
60-
runCfg = emptyConfig()
61-
}
62-
let toComplete = command.pop() || ""
63-
64-
const suggestions = await cli.complete({
65-
jwt: runCfg.jwt,
66-
leftWords: command,
67-
rightWords: [],
68-
selectedWord: toComplete
69-
})
70-
expected.forEach(suggestion => expect(suggestions).toContain(suggestion))
59+
const resp = await cli.run(runCfg, command)
60+
expect(resp.exitCode).toBeGreaterThan(0)
61+
expect(resp.stderr).toMatch(expected)
62+
}
63+
64+
const complete = async (
65+
expected: string[],
66+
command: string[],
67+
runCfg: RunConfig | null = null,
68+
) => {
69+
if (runCfg === null) {
70+
runCfg = emptyConfig()
7171
}
72+
let toComplete = command.pop() || ''
7273

73-
it('can run cli commands', async () => run(/profile.*default/, ['info']))
74+
const suggestions = await cli.complete({
75+
jwt: runCfg.jwt,
76+
leftWords: command,
77+
rightWords: [],
78+
selectedWord: toComplete,
79+
})
80+
expected.forEach(suggestion => expect(suggestions).toContain(suggestion))
81+
}
7482

75-
it('can run help', async () => runWithError(/USAGE:\n.*scw <command>.*/, []))
83+
it('can run cli commands', async () => run(/profile.*default/, ['info']))
7684

77-
it('can use jwt', async () => runWithError(/.*denied authentication.*invalid JWT.*/, ['instance', 'server', 'list']))
85+
it('can run help', async () => runWithError(/USAGE:\n.*scw <command>.*/, []))
7886

79-
it('can complete', async () => complete(['server', 'image', 'volume'], ['instance', '']))
87+
it('can use jwt', async () =>
88+
runWithError(/.*denied authentication.*invalid JWT.*/, [
89+
'instance',
90+
'server',
91+
'list',
92+
]))
8093

81-
it('can configure terminal size', async () => {
82-
const runCfg = emptyConfig()
94+
it('can complete', async () =>
95+
complete(['server', 'image', 'volume'], ['instance', '']))
8396

84-
await cli.configureOutput({width: 100, color: false})
85-
const resp = await cli.run(runCfg, ['marketplace', 'image', 'list'])
86-
expect(resp.exitCode).toBe(0)
97+
it('can configure terminal size', async () => {
98+
const runCfg = emptyConfig()
8799

88-
const lines = resp.stdout.split("\n")
89-
expect(lines.length).toBeGreaterThan(1)
90-
expect(lines[2].length).toBeLessThan(100)
91-
})
100+
await cli.configureOutput({ width: 100, color: false })
101+
const resp = await cli.run(runCfg, ['marketplace', 'image', 'list'])
102+
expect(resp.exitCode).toBe(0)
92103

93-
it('can enable colors', async () => {
94-
const runCfg = emptyConfig()
104+
const lines = resp.stdout.split('\n')
105+
expect(lines.length).toBeGreaterThan(1)
106+
expect(lines[2].length).toBeLessThan(100)
107+
})
95108

96-
await cli.configureOutput({width: 100, color: false})
97-
const resp = await cli.run(runCfg, ['invalid'])
98-
await cli.configureOutput({width: 100, color: true})
99-
const coloredResp = await cli.run(runCfg, ['invalid'])
109+
it('can enable colors', async () => {
110+
const runCfg = emptyConfig()
100111

101-
expect(coloredResp.stderr.length).toBeGreaterThan(resp.stderr.length)
102-
expect(coloredResp.stderr).not.toEqual(resp.stderr)
103-
})
112+
await cli.configureOutput({ width: 100, color: false })
113+
const resp = await cli.run(runCfg, ['invalid'])
114+
await cli.configureOutput({ width: 100, color: true })
115+
const coloredResp = await cli.run(runCfg, ['invalid'])
104116

105-
afterAll(async () => {
106-
try {
107-
await cli.stop()
108-
go._resume()
109-
} catch (e) {
110-
console.log(e)
111-
}
112-
})
117+
expect(coloredResp.stderr.length).toBeGreaterThan(resp.stderr.length)
118+
expect(coloredResp.stderr).not.toEqual(resp.stderr)
119+
})
120+
121+
afterAll(async () => {
122+
try {
123+
await cli.stop()
124+
go._resume()
125+
} catch (e) {
126+
console.log(e)
127+
}
128+
})
113129
})

wasm/src/jshelpers.test.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
2-
import {describe, it, expect, afterAll, beforeAll} from 'vitest'
1+
import { describe, it, expect, afterAll, beforeAll } from 'vitest'
32

43
import '../wasm_exec_node.cjs'
54
import '../wasm_exec.cjs'
6-
import {RunConfig} from '../cli'
5+
import { RunConfig } from '../cli'
76
import * as fs from 'fs'
8-
import {loadWasmBinary} from "./utils";
7+
import { loadWasmBinary } from './utils'
98

109
type CLITester = {
11-
stop: () => Promise<void>
12-
FromSlice: () => string[]
10+
stop: () => Promise<void>
11+
FromSlice: () => string[]
1312
}
1413

1514
describe('With test environment', async () => {
16-
let cli: CLITester
17-
18-
beforeAll(async () => {
19-
// @ts-ignore
20-
cli = await loadWasmBinary('./cliTester.wasm') as CLITester
21-
})
22-
23-
it('can return array', async () => {
24-
const array = cli.FromSlice()
25-
26-
expect(array).toHaveLength(3)
27-
expect(array).toContain("1")
28-
expect(array).toContain("2")
29-
expect(array).toContain("3")
30-
})
31-
32-
afterAll(async () => {
33-
try {
34-
await cli.stop()
35-
go._resume()
36-
} catch (e) {
37-
console.log(e)
38-
}
39-
})
40-
})
15+
let cli: CLITester
16+
17+
beforeAll(async () => {
18+
// @ts-ignore
19+
cli = (await loadWasmBinary('./cliTester.wasm')) as CLITester
20+
})
21+
22+
it('can return array', async () => {
23+
const array = cli.FromSlice()
24+
25+
expect(array).toHaveLength(3)
26+
expect(array).toContain('1')
27+
expect(array).toContain('2')
28+
expect(array).toContain('3')
29+
})
30+
31+
afterAll(async () => {
32+
try {
33+
await cli.stop()
34+
go._resume()
35+
} catch (e) {
36+
console.log(e)
37+
}
38+
})
39+
})

0 commit comments

Comments
 (0)