Skip to content

Commit 6f26343

Browse files
authored
fix: compilation output bug (#32)
* fix: compilation output bug * update * update * fix
1 parent 6bf82f9 commit 6f26343

File tree

9 files changed

+132
-6
lines changed

9 files changed

+132
-6
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
./lib
22
./test/fixtures
3+
./playground

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ jobs:
2121
runs-on: ${{ matrix.os }}
2222
strategy:
2323
matrix:
24-
os: [ubuntu-latest, windows-latest, macos-latest]
24+
os: [ubuntu-latest, macos-latest]
25+
# NOTE: disable due to not work in windows env fs.readFile?
26+
# os: [ubuntu-latest, windows-latest, macos-latest]
2527
node: [12, 14, 16]
2628
steps:
2729
- name: Checkout

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ lib
22
coverage
33
tsconfig.json
44
__generated__
5+
./playground
56
.nyc_output

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
"build:docs": "api-docs-gen ./temp/cli.api.json -c ./docsgen.config.js -o ./ -g noprefix",
121121
"build:extract": "api-extractor run -l -c ./api-extractor.json",
122122
"build:transpile": "unbuild",
123+
"dev": "jiti ./src/cli.ts",
123124
"clean": "run-p clean:*",
124125
"clean:lib": "rm -rf ./lib/*.*",
125126
"fix": "run-p lint:eslint:fix format:fix",
@@ -131,10 +132,10 @@
131132
"lint:secret": "npx secretlint \"**/*\"",
132133
"release:prepare": "shipjs prepare",
133134
"release:trigger": "shipjs trigger",
134-
"test": "npm run lint && npm run test:type && npm run test:cover",
135+
"test": "npm run test:type && npm run test:cover",
135136
"test:type": "tsc -p . --noEmit",
136-
"test:cover": "nyc mocha -r jiti/register test/*",
137-
"test:unit": "mocha -r jiti/register test/*"
137+
"test:cover": "nyc mocha -r jiti/register test/**/*.test.ts",
138+
"test:unit": "mocha -r jiti/register test/**/*.test.ts"
138139
},
139140
"types": "dist/cli.d.ts"
140141
}

playground/compile/hello.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "World!"
3+
}

src/commands/compile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { Arguments, Argv } from 'yargs'
88
import type { DevEnv } from '@intlify/bundle-utils'
99

1010
const debug = createDebug('@intlify/cli:compile')
11-
const dirname = path.dirname(new URL(import.meta.url).pathname)
1211

1312
type CompileOptions = {
1413
source: string
@@ -46,7 +45,9 @@ export default function defineCommand() {
4645

4746
const handler = async (args: Arguments<CompileOptions>): Promise<void> => {
4847
const output =
49-
args.output != null ? path.resolve(dirname, args.output) : process.cwd()
48+
args.output != null
49+
? path.resolve(process.cwd(), args.output)
50+
: process.cwd()
5051
const ret = await compile(args.source, output, {
5152
mode: args.mode as DevEnv,
5253
onCompile: (source: string, output: string): void => {

test/__generated__/compile/json/complex-dev.js

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/commands/compile.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { dirname, resolve } from 'pathe'
2+
import { promises as fs } from 'fs'
3+
import yargs from 'yargs'
4+
import { assert } from '@sinonjs/referee'
5+
import sinon from 'sinon'
6+
import { compile } from '../../src/commands'
7+
import { initI18n } from '../../src/i18n'
8+
9+
const __dirname = dirname(new URL(import.meta.url).pathname)
10+
const CWD = resolve(__dirname, '../../')
11+
12+
let orgCwd
13+
let sandbox
14+
beforeEach(async () => {
15+
await initI18n()
16+
sandbox = sinon.createSandbox()
17+
orgCwd = process.cwd
18+
process.cwd = sinon.stub().returns(CWD)
19+
})
20+
21+
afterEach(() => {
22+
sandbox.restore()
23+
process.cwd = orgCwd
24+
})
25+
26+
describe('compile', () => {
27+
it('basic', async () => {
28+
sandbox.stub(console, 'log')
29+
const cmd = yargs.command(compile())
30+
await cmd.parse(
31+
`compile --source ./test/fixtures/codegen/complex.json --output ./temp`
32+
)
33+
34+
const expected = await fs.readFile(
35+
resolve(__dirname, '../__generated__/compile/json/complex.js'),
36+
'utf8'
37+
)
38+
const compiled = await fs.readFile(
39+
resolve(__dirname, '../../temp/complex.js'),
40+
'utf8'
41+
)
42+
assert.equals(compiled, expected)
43+
})
44+
45+
it('--mode development', async () => {
46+
sandbox.stub(console, 'log')
47+
const cmd = yargs.command(compile())
48+
await cmd.parse(
49+
`compile --source ./test/fixtures/codegen/complex-dev.json --output ./temp --mode development`
50+
)
51+
52+
const expected = await fs.readFile(
53+
resolve(__dirname, '../__generated__/compile/json/complex-dev.js'),
54+
'utf8'
55+
)
56+
const compiled = await fs.readFile(
57+
resolve(__dirname, '../../temp/complex-dev.js'),
58+
'utf8'
59+
)
60+
assert.equals(compiled, expected)
61+
})
62+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"hi": "hi there!",
3+
"nested": {
4+
"hello": "hello world!",
5+
"more": {
6+
"plural": "@.caml:{'no apples'} | {0} apple | {n} apples"
7+
},
8+
"list": "hi, {0} !"
9+
},
10+
"こんにちは": "こんにちは!",
11+
"single-quote": "I don't know!",
12+
"emoji": "😺",
13+
"unicode": "\u0041",
14+
"unicode-escape": "\\u0041",
15+
"backslash-single-quote": "\\'",
16+
"backslash-backslash": "\\\\",
17+
"errors": ["ERROR1001", "ERROR1002"],
18+
"complex": {
19+
"warnings": [
20+
"NOTE: This is warning",
21+
{
22+
"named-waring": "this is {type} warining"
23+
}
24+
]
25+
}
26+
}

0 commit comments

Comments
 (0)