Skip to content

Commit 15572b5

Browse files
Merge pull request #8 from oslabs-beta/exportTypescriptProject
Export typescript project
2 parents d0f484d + ebccfb2 commit 15572b5

File tree

1 file changed

+74
-21
lines changed

1 file changed

+74
-21
lines changed

src/components/file_system_interface/ExportProject.vue

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,41 @@ export default {
4444
* createExport(this.componentMap['App'].children)
4545
* */
4646
createRouter(location) {
47-
fs.writeFileSync(
48-
path.join(location, "src", "router.js"),
49-
this.createRouterImports(this.componentMap["App"].children) +
50-
this.createExport(this.componentMap["App"].children)
51-
);
47+
if (this.exportAsTypescript === "on") {
48+
fs.writeFileSync(
49+
path.join(location, "src", "router.ts"),
50+
this.createRouterImports(this.componentMap["App"].children) +
51+
this.createExport(this.componentMap["App"].children)
52+
);
53+
} else {
54+
fs.writeFileSync(
55+
path.join(location, "src", "router.js"),
56+
this.createRouterImports(this.componentMap["App"].children) +
57+
this.createExport(this.componentMap["App"].children)
58+
);
59+
}
5260
},
5361
/**
5462
* @description import routed components from the /views/ dir
5563
* @argument: this.componentMap['App'].children
5664
*/
5765
createRouterImports(appChildren) {
58-
let str = "import { createRouter, createWebHistory } from 'vue-router'\n";
66+
let str = "import { createRouter, createWebHistory } from 'vue-router';\n";
5967
appChildren.forEach((child) => {
60-
str += `import ${child} from './views/${child}.vue'\n`;
68+
str += `import ${child} from './views/${child}.vue';\n`;
6169
});
6270
return str;
6371
},
6472
/**
6573
* @description creates the `export default` code in <script>
6674
*/
6775
createExport(appChildren) {
68-
let str =
69-
"export default createRouter({\n\thistory: createWebHistory(),\n\tbase: process.env.BASE_URL,\n\troutes: [\n";
76+
let str;
77+
if (this.exportAsTypescript === 'on') {
78+
str = "export default createRouter({\n\thistory: createWebHistory(process.env.BASE_URL),\n\troutes: [\n";
79+
} else {
80+
str = "export default createRouter({\n\thistory: createWebHistory(),\n\tbase: process.env.BASE_URL,\n\troutes: [\n";
81+
}
7082
appChildren.forEach((child) => {
7183
if (child === "HomeView") {
7284
str += `\t\t{\n\t\t\tpath: '/',\n\t\t\tname:'${child}',\n\t\t\tcomponent:${child}\n\t\t},\n`;
@@ -211,7 +223,11 @@ export default {
211223
imports += "mapState, mapActions";
212224
} else if (currentComponent.state.length) imports += "mapState";
213225
else imports += "mapActions";
214-
imports += ' } from "vuex"\n';
226+
imports += ' } from "vuex";\n';
227+
}
228+
// if in Typescript mode, import defineComponent
229+
if (this.exportAsTypescript === "on") {
230+
imports += 'import { defineComponent } from "vue";\n';
215231
}
216232
// add imports for children
217233
children.forEach((name) => {
@@ -258,16 +274,25 @@ export default {
258274
methods += " },\n";
259275
}
260276
// concat all code within script tags
261-
let output = "\n\n<script>\n";
262-
output +=
263-
imports + "\nexport default {\n name: '" + componentName + "'";
277+
let output;
278+
if (this.exportAsTypescript === 'on') {
279+
output = "\n\n<script lang='ts'>\n";
280+
output += imports + "\nexport default defineComponent ({\n name: '" + componentName + "'";
281+
} else {
282+
output = "\n\n<script>\n";
283+
output += imports + "\nexport default {\n name: '" + componentName + "'";
284+
}
264285
output += ",\n components: {\n";
265286
output += childrenComponentNames + " },\n";
266287
output += data;
267288
output += computed;
268289
output += methods;
269290
// eslint-disable-next-line no-useless-escape
270-
output += "};\n<\/script>";
291+
if (this.exportAsTypescript === 'on') {
292+
output += "});\n<\/script>";
293+
} else {
294+
output += "};\n<\/script>";
295+
}
271296
return output;
272297
} else {
273298
let str = "";
@@ -279,6 +304,9 @@ export default {
279304
childrenComponentNames += ` ${name},\n`;
280305
});
281306
// eslint-disable-next-line no-useless-escape
307+
if (this.exportAsTypescript === "on") {
308+
return `\n\n<script lang="ts">\nimport { defineComponent } from "vue";\n ${str}\nexport default defineComponent ({\n name: '${componentName}',\n components: {\n${childrenComponentNames} }\n});\n<\/script>`;
309+
}
282310
return `\n\n<script>\n${str}\nexport default {\n name: '${componentName}',\n components: {\n${childrenComponentNames} }\n};\n<\/script>`;
283311
}
284312
},
@@ -315,15 +343,20 @@ export default {
315343
},
316344
// creates main.js boilerplate
317345
createMainFile(location) {
318-
let str = `import { createApp } from 'vue'`;
319-
str += `\nimport App from './App.vue'`;
320-
str += `\nimport router from './router'`;
346+
let str = `import { createApp } from 'vue';`;
347+
str += `\nimport App from './App.vue';`;
348+
str += `\nimport router from './router';`;
321349
// str += `\n\n import './index.css'`
322-
str += `\n\n const app = createApp(App)`;
350+
str += `\n\n const app = createApp(App);`;
323351
// str += `\n\trouter,
324352
str += `\napp.use(router);`;
325353
str += `\n app.mount('#app');`;
326-
fs.writeFileSync(path.join(location, "src", "main.js"), str);
354+
// if using typescript, export with .ts extension
355+
if (this.exportAsTypescript === "on") {
356+
fs.writeFileSync(path.join(location, "src", "main.ts"), str);
357+
} else {
358+
fs.writeFileSync(path.join(location, "src", "main.js"), str);
359+
}
327360
},
328361
// create babel file
329362
createBabel(location) {
@@ -332,7 +365,20 @@ export default {
332365
str += `\n\t\t'@vue/app'`;
333366
str += `\n\t]`;
334367
str += `\n}`;
335-
fs.writeFileSync(path.join(location, "babel.config.js"), str);
368+
// if using typescript, export with .ts extension
369+
if (this.exportAsTypescript === "on") {
370+
fs.writeFileSync(path.join(location, "src", "babel.config.ts"), str);
371+
} else {
372+
fs.writeFileSync(path.join(location, "babel.config.js"), str);
373+
}
374+
},
375+
createTSConfig(location) {
376+
if (this.exportAsTypescript === "on") {
377+
let str = `{\n\t"extends": "@vue/tsconfig/tsconfig.web.json",\n\t"include": ["src/**/*", "src/**/*.vue"],\n\t"compilerOptions": {\n\t\t"baseUrl": ".",\n\t\t"paths": {\n\t\t\t"@/*": ["./src/*"]\n\t\t}\n\t}\n}`;
378+
fs.writeFileSync(path.join(location, "tsconfig.json"), str);
379+
} else {
380+
return;
381+
}
336382
},
337383
// create package.json file
338384
createPackage(location) {
@@ -358,6 +404,12 @@ export default {
358404
str += `\n\t\t"eslint": "^6.7.2",`;
359405
str += `\n\t\t"eslint-plugin-vue": "^7.0.0-0",`;
360406
str += `\n\t\t"@vue/compiler-sfc": "^3.0.0-0"`;
407+
if (this.exportAsTypescript === "on") {
408+
str += `,\n\t\t"@vue/tsconfig": "^0.1.3",`;
409+
str += `\n\t\t"typescript": "~4.5.5",`;
410+
str += `\n\t\t"vue-tsc": "^0.31.4",`;
411+
str += `\n\t\t"@babel/preset-typescript": "^7.16.7"`; // not sure we need this?
412+
}
361413
str += `\n\t},`;
362414
str += `\n\t"eslintConfig": {`;
363415
str += `\n\t\t"root": true,`;
@@ -401,6 +453,7 @@ export default {
401453
this.createIndexFile(data);
402454
this.createMainFile(data);
403455
this.createBabel(data);
456+
this.createTSConfig(data);
404457
this.createPackage(data);
405458
// exports images to the /assets folder
406459
// eslint-disable-next-line no-unused-vars
@@ -444,7 +497,7 @@ export default {
444497
},
445498
},
446499
computed: {
447-
...mapState(["componentMap", "imagePath", "routes"]),
500+
...mapState(["componentMap", "imagePath", "routes", "exportAsTypescript"]),
448501
},
449502
};
450503
</script>

0 commit comments

Comments
 (0)