Skip to content

Commit f020598

Browse files
authored
feat: support initialization with pinia (#143)
* feat: support initialization with pinia * fix: format * fix: remove comment
1 parent ff639df commit f020598

File tree

9 files changed

+103
-49
lines changed

9 files changed

+103
-49
lines changed

packages/cli-test-utils/getPluginConfig.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,12 @@ import {
77
federationOption,
88
PluginChoiceOption,
99
} from '../cli/src/config/plugins'
10+
import { defaultOptions } from '../cli/src/commands/init'
1011

11-
export const defaultOptions: any = {
12-
name: '',
13-
version: '1.0.0',
14-
license: 'ISC',
15-
plugins: [],
16-
test: 'none',
12+
export const defaultOptionsForTest: any = {
13+
...defaultOptions,
1714
// NOTE: the default value of federationType is only used in test
1815
federationType: 'Host',
19-
pagesPluginImported: false,
20-
globalStylePluginImported: false,
21-
componentsPluginImported: false,
22-
contentPluginImported: false,
23-
markdownPluginImported: false,
24-
federationPluginImported: false,
2516
}
2617

2718
const TO_BE_ADDED_PLUGINS: PluginChoiceOption[] = [
@@ -50,7 +41,7 @@ const pluginCompositions: Array<PluginChoiceOption[]> = getPluginCompositions()
5041
export function getConfigs(includes?: string[], excludes?: string[]): any[] {
5142
const resultConfigs: any[] = []
5243
pluginCompositions.forEach((composition, index) => {
53-
const config: any = Object.assign({}, defaultOptions)
44+
const config: any = Object.assign({}, defaultOptionsForTest)
5445
config.name = `test_plugins_${index + 1}`
5546
config.plugins = composition
5647
config.plugins.forEach((plugin: PluginChoiceOption) => {

packages/cli/__tests__/__snapshots__/cli.spec.ts.snap

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@ Commands:
1717
"
1818
`;
1919
20-
exports[`ori -h > A1 1`] = `
21-
"Usage: ori <command> [options]
22-
23-
Options:
24-
-h, --help display help for command
25-
26-
Commands:
27-
init [options] <app-name> init a new project
28-
dev [options] alias of \\"ori dev\\" in the current project
29-
build alias of \\"ori build\\" in the current project
30-
tovite [options] use vite in the current project
31-
tovue3 [options] use vue-next in the current project
32-
help [command] display help for command
33-
"
34-
`;
35-
3620
exports[`ori build --help > A7 1`] = `
3721
"Usage: ori build [options]
3822
@@ -187,7 +171,7 @@ exports[`ori init with all plugins > A3 3`] = `
187171
\\"vue\\": \\"^3.0.5\\",
188172
\\"vue-i18n\\": \\"^9.2.0-beta.15\\",
189173
\\"vue-router\\": \\"^4.0.11\\",
190-
\\"vuex\\": \\"^4.0.2\\"
174+
\\"pinia\\": \\"^2.0.13\\"
191175
},
192176
\\"devDependencies\\": {
193177
\\"@originjs/vite-plugin-global-style\\": \\"^1.0.2\\",
@@ -384,7 +368,7 @@ exports[`ori init without plugins > A4 3`] = `
384368
\\"vue\\": \\"^3.0.5\\",
385369
\\"vue-i18n\\": \\"^9.2.0-beta.15\\",
386370
\\"vue-router\\": \\"^4.0.11\\",
387-
\\"vuex\\": \\"^4.0.2\\"
371+
\\"pinia\\": \\"^2.0.13\\"
388372
},
389373
\\"devDependencies\\": {
390374
\\"@intlify/vue-i18n-loader\\": \\"^3.0.0\\",

packages/cli/__tests__/cli.spec.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import create from '../../cli-test-utils/createTestProject'
77
import runServer from '../../cli-test-utils/createTestProjectServer'
88
import runBuild from '../../cli-test-utils/buildTestProject'
99
import {
10-
defaultOptions,
10+
defaultOptionsForTest,
1111
getConfigs,
1212
} from '../../cli-test-utils/getPluginConfig'
1313
import { formatToLf } from '../src/utils/formatCrlf'
@@ -156,6 +156,43 @@ test('ori init without plugins', async () => {
156156
expect(project.has('src/assets/originjs_readme.md')).toEqual(false)
157157
}, 10000)
158158

159+
test('ori init with store utils', async () => {
160+
const storeConfigs = ['pinia', 'vuex', 'none']
161+
const ProjectPath = path.join(
162+
process.cwd(),
163+
'packages',
164+
'cli-test-utils',
165+
DEMO_PATH,
166+
)
167+
168+
for (const value of storeConfigs) {
169+
const config = Object.assign({}, defaultOptionsForTest, { store: value })
170+
await initializeModules(`store_utils_${value}`, config, true, ProjectPath)
171+
try {
172+
// skip files
173+
expect(
174+
fs.pathExistsSync(
175+
path.join(ProjectPath, `store_utils_${value}`, 'src/store'),
176+
),
177+
).toEqual(value !== 'none')
178+
if (value === 'none') {
179+
continue
180+
}
181+
const storeContent = formatToLf(fs.readFileSync(
182+
path.join(ProjectPath, `store_utils_${value}`, 'src/store/index.ts'), 'utf-8'))
183+
if (value === 'pinia') {
184+
expect(storeContent).toMatch(results.storeCreationImportWithPinia)
185+
expect(formatToLf(storeContent)).toMatch(results.storeCreationWithPinia)
186+
} else if (value === 'store') {
187+
expect(storeContent).toMatch(results.storeCreationImportWithVuex)
188+
expect(formatToLf(storeContent)).toMatch(results.storeCreationWithVuex)
189+
}
190+
} finally {
191+
fs.remove(path.join(ProjectPath, `store_utils_${value}`))
192+
}
193+
}
194+
}, 30000)
195+
159196
test('ori init with test utils', async () => {
160197
const testConfigs = ['none', 'jest', 'vitest']
161198
const ProjectPath = path.join(
@@ -166,13 +203,13 @@ test('ori init with test utils', async () => {
166203
)
167204

168205
for (const value of testConfigs) {
169-
const config = Object.assign({}, defaultOptions, { test: value })
206+
const config = Object.assign({}, defaultOptionsForTest, { test: value })
170207
await initializeModules(`test_utils_${value}`, config, true, ProjectPath)
171-
const packageJsonContent = fs.readFileSync(
172-
path.join(ProjectPath, `test_utils_${value}`, 'package.json'), 'utf-8')
173-
const viteConfigContent = formatToLf(fs.readFileSync(
174-
path.join(ProjectPath, `test_utils_${value}`, 'vite.config.ts'), 'utf-8'))
175208
try {
209+
const packageJsonContent = fs.readFileSync(
210+
path.join(ProjectPath, `test_utils_${value}`, 'package.json'), 'utf-8')
211+
const viteConfigContent = formatToLf(fs.readFileSync(
212+
path.join(ProjectPath, `test_utils_${value}`, 'vite.config.ts'), 'utf-8'))
176213
if (value === 'jest') {
177214
expect(packageJsonContent).toMatch(results.packageJsonScriptWithJest)
178215
} else if (value === 'vitest') {

packages/cli/__tests__/results.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ const viteConfigWithVitest = `test: {
6666
global: true
6767
}`
6868

69+
const storeCreationImportWithVuex = `import { createStore } from 'vuex'`
70+
const storeCreationImportWithPinia = `import { createPinia } from 'pinia'`
71+
const storeCreationWithVuex = `export default createStore({
72+
state: {},
73+
mutations: {},
74+
actions: {},
75+
modules: {},
76+
})`
77+
const storeCreationWithPinia = `export default createPinia()`
78+
6979
const serverRunning = `dev server running at:`
7080
const serverUpdated = `hmr update /src/App.vue`
7181

@@ -101,6 +111,10 @@ export default {
101111
packageJsonScriptWithJest,
102112
packageJsonScriptWithVitest,
103113
viteConfigWithVitest,
114+
storeCreationImportWithVuex,
115+
storeCreationImportWithPinia,
116+
storeCreationWithPinia,
117+
storeCreationWithVuex,
104118
serverRunning,
105119
serverUpdated,
106120
}

packages/cli/oriTemplate/__package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
"vue": "^3.0.5",
2424
"vue-i18n": "^9.2.0-beta.15",
2525
"vue-router": "^4.0.11",
26+
<%_ if (store == 'pinia') { _%>
27+
"pinia": "^2.0.13"
28+
<%_ } else if (store === 'vuex') { _%>
2629
"vuex": "^4.0.2"
30+
<%_ } _%>
2731
},
2832
"devDependencies": {
2933
<%_ plugins.forEach(function(plugin) { _%>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%_ if (store === 'pinia') { _%>
2+
import { createPinia } from 'pinia'
3+
4+
export default createPinia()
5+
<%_ } else if (store === 'vuex') { _%>
6+
import { createStore } from 'vuex'
7+
8+
export default createStore({
9+
state: {},
10+
mutations: {},
11+
actions: {},
12+
modules: {},
13+
})
14+
<%_ } _%>

packages/cli/oriTemplate/src/store/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/cli/src/commands/init.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ type InitCliOptions = {
2121
uninstalled?: boolean
2222
}
2323

24-
const defaultOptions: any = {
24+
export const defaultOptions: any = {
2525
name: '',
2626
version: '1.0.0',
2727
license: 'ISC',
2828
test: 'none',
29+
store: 'pinia',
2930
plugins: [],
3031
pagesPluginImported: false,
3132
globalStylePluginImported: false,
@@ -54,6 +55,7 @@ export function cpdir(
5455
fs.mkdirSync(path.join(dirNew, name))
5556
resolve('Successfully created the folder!')
5657
dirNew = path.join(dirNew, name)
58+
// file or directory that will be skipped
5759
const skipFiles: Array<string> = getSkipFilesWithRelativePath()
5860
walkDir(dirOld, dirNew, skipFiles)
5961
} catch (err) {
@@ -65,13 +67,20 @@ export function cpdir(
6567
function getSkipFilesWithRelativePath(): Array<string> {
6668
const skipFiles = []
6769

70+
// test
6871
if (config.test !== 'jest') {
6972
skipFiles.push('babel.config.js', 'jest.config.js', 'shim.d.ts')
7073
}
7174
if (config.test === 'none') {
7275
skipFiles.push('test')
7376
}
7477

78+
// store
79+
if (config.store === 'none') {
80+
skipFiles.push('src/store')
81+
}
82+
83+
// plugins
7584
if (!config.pagesPluginImported) {
7685
skipFiles.push(
7786
'src/pages/users',

packages/cli/src/config/prompt.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export const promptList: Array<QuestionCollection> = [
3535
},
3636
{
3737
type: 'list',
38-
message: 'Please choose the test utils you want to use:',
39-
name: 'test',
40-
choices: ['none', 'jest', 'vitest'],
38+
message: 'Please choose Vue Store:',
39+
name: 'store',
40+
choices: ['pinia', 'vuex', 'none'],
4141
filter: (val: any) => {
4242
return val
4343
},
@@ -102,4 +102,13 @@ export const promptList: Array<QuestionCollection> = [
102102
return federationSelected
103103
},
104104
},
105+
{
106+
type: 'list',
107+
message: 'Please choose the test utils you want to use:',
108+
name: 'test',
109+
choices: ['none', 'jest', 'vitest'],
110+
filter: (val: any) => {
111+
return val
112+
},
113+
},
105114
]

0 commit comments

Comments
 (0)