Skip to content

Commit 750115b

Browse files
Native Only improvements (#24)
* created reusable dir structure file lookup created reusable multi-line string replacement in files for native only - remove 'web'/'native' template tags * Updated templates for native Only use Remove .native.vue files from native only projects * version bump to 0.0.8
1 parent d1fd1ca commit 750115b

File tree

7 files changed

+202
-101
lines changed

7 files changed

+202
-101
lines changed

Diff for: generator/index.js

+105-23
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ module.exports = async (api, options, rootOptions) => {
187187
// render App_Resources folder
188188
api.render(async () => {
189189
// eslint-disable-next-line prettier/prettier
190-
await renderDirectory(
190+
await renderDirectoryStructure(
191191
api,
192192
options,
193193
rootOptions,
@@ -202,7 +202,7 @@ module.exports = async (api, options, rootOptions) => {
202202
if (!options.isNativeOnly) {
203203
api.render(async () => {
204204
// render src directory
205-
await renderDirectory(
205+
await renderDirectoryStructure(
206206
api,
207207
options,
208208
rootOptions,
@@ -222,7 +222,7 @@ module.exports = async (api, options, rootOptions) => {
222222
// Is Native Only
223223
api.render(async () => {
224224
// render app directory
225-
await renderDirectory(
225+
await renderDirectoryStructure(
226226
api,
227227
options,
228228
rootOptions,
@@ -247,6 +247,11 @@ module.exports = async (api, options, rootOptions) => {
247247
// create nsconfig.json in ./ or ./ns-example
248248
nsconfigSetup(genConfig.dirPathPrefix, api.resolve('nsconfig.json'), genConfig.nativeAppPathModifier, genConfig.appResourcesPathModifier);
249249

250+
// copy over .vue with native.vue files
251+
if (options.isNativeOnly) {
252+
nativeOnlyRenameFiles(genConfig.dirPathPrefix + genConfig.nativeAppPathModifier.slice(0, -1));
253+
}
254+
250255
if (api.hasPlugin('typescript')) {
251256
if (fs.existsSync(api.resolve('tslint.json'))) {
252257
require('../lib/tslint')({}, api, true);
@@ -581,6 +586,53 @@ const nsconfigSetup = (module.exports.nsconfigSetup = async (dirPathPrefix, nsco
581586
}
582587
});
583588

589+
// can be used to strip out template tags in native only project
590+
// currently unused in preference for EJS templating
591+
// eslint-disable-next-line no-unused-vars
592+
const stripTemplateTags = (module.exports.stripTemplateTags = async (srcPathPrefix) => {
593+
try {
594+
const files = await getAllFilesInDirStructure(srcPathPrefix, '');
595+
596+
for (const file of files) {
597+
if (file.slice(-4) == '.vue') {
598+
const options = {
599+
files: path.join(srcPathPrefix, file),
600+
from: [
601+
new RegExp(`^((<template)(.+\\bweb\\b))([\\s\\S]*?>)[\\s\\S]*?(<\\/template>)`, `gim`),
602+
new RegExp(`^((<template)(.+\\bnative\\b))([\\s\\S]*?>)`, `gim`)
603+
],
604+
to: ['', '<template>']
605+
};
606+
607+
await replaceInFile(options);
608+
}
609+
}
610+
} catch (err) {
611+
throw err;
612+
}
613+
});
614+
615+
const nativeOnlyRenameFiles = (module.exports.nativeOnlyRenameFiles = async (srcPathPrefix) => {
616+
try {
617+
const _files = await getAllFilesInDirStructure(srcPathPrefix, '');
618+
const files = new Array();
619+
const match = '.native.vue';
620+
621+
for (const file of _files) {
622+
if (file.slice(-11) == match) {
623+
files.push(path.join(srcPathPrefix, file));
624+
}
625+
}
626+
627+
for (const file of files) {
628+
const oldFile = file.replace(match, '.vue');
629+
fs.moveSync(file, oldFile, { overwrite: true });
630+
}
631+
} catch (err) {
632+
throw err;
633+
}
634+
});
635+
584636
// setup tslintSetup
585637
const nativeOnlyPackageJsonSetup = (module.exports.nativeOnlyPackageJsonSetup = async (filePath) => {
586638
let fileContents = '';
@@ -719,17 +771,6 @@ const tsconfigSetup = (module.exports.tsconfigSetup = async (options, dirPathPre
719771
}
720772
});
721773

722-
// extract callsite file location using error stack
723-
const extractCallDir = (module.exports.extractCallDir = () => {
724-
try {
725-
const obj = {};
726-
Error.captureStackTrace(obj);
727-
return path.dirname(obj.stack.split('\n')[3].match(/\s\((.*):\d+:\d+\)$/)[1]);
728-
} catch (err) {
729-
throw err;
730-
}
731-
});
732-
733774
// Use the generator's render function to render individual files passed in from an array.
734775
// Will iterate through the array and then construct and object that is passed to render()
735776
const renderFilesIndividually = (module.exports.renderFilesIndividually = async (
@@ -766,16 +807,22 @@ const renderFilesIndividually = (module.exports.renderFilesIndividually = async
766807
// function lacks a simple directory in and directory out option. So, we have to get the contents
767808
// of the passed in directory and then render each file individually to where we want it via
768809
// the render function's isObject(source) option that we use in our renderFilesIndividually function.
769-
const renderDirectory = (module.exports.renderDirectory = async (api, options, rootOptions, jsOrTs, commonRenderOptions, srcPathPrefix, destPathPrefix) => {
810+
811+
// eslint-disable-next-line prettier/prettier
812+
// eslint-disable-next-line max-len
813+
const renderDirectoryStructure = (module.exports.renderDirectoryStructure = async (
814+
api,
815+
options,
816+
rootOptions,
817+
jsOrTs,
818+
commonRenderOptions,
819+
srcPathPrefix,
820+
destPathPrefix
821+
) => {
770822
try {
771-
const baseDir = await extractCallDir();
772-
const source = path.resolve(baseDir, srcPathPrefix);
773823
const files = new Array();
774-
775-
const globby = require('globby');
776-
const _files = await globby(['**/*'], {
777-
cwd: source
778-
});
824+
const baseDir = await extractCallDir()
825+
const _files = await getAllFilesInDirStructure(srcPathPrefix, baseDir);
779826

780827
for (const rawPath of _files) {
781828
// // // let filename = path.basename(rawPath);
@@ -820,13 +867,48 @@ const renderDirectory = (module.exports.renderDirectory = async (api, options, r
820867
files.push(rawPath);
821868
}
822869
}
823-
824870
renderFilesIndividually(api, options, jsOrTs, files, commonRenderOptions, srcPathPrefix, destPathPrefix);
825871
} catch (err) {
826872
throw err;
827873
}
828874
});
829875

876+
// extract callsite file location using error stack
877+
const extractCallDir = (module.exports.extractCallDir = () => {
878+
try {
879+
const obj = {};
880+
Error.captureStackTrace(obj);
881+
return path.dirname(obj.stack.split('\n')[3].match(/\s\((.*):\d+:\d+\)$/)[1]);
882+
} catch (err) {
883+
throw err;
884+
}
885+
});
886+
887+
// utility function used to get all the files in a directory structure. is recursive in nature due to globby
888+
const getAllFilesInDirStructure = (module.exports.replaceInFile = async (srcPathPrefix, baseDir) => {
889+
try {
890+
const source = path.resolve(baseDir, srcPathPrefix);
891+
const globby = require('globby');
892+
const _files = await globby(['**/*'], {
893+
cwd: source
894+
});
895+
896+
return _files;
897+
} catch (error) {
898+
console.log(error);
899+
}
900+
});
901+
902+
// utility function used to remove sections of strings from files
903+
const replaceInFile = (module.exports.replaceInFile = async (options) => {
904+
try {
905+
//const changes =
906+
await replace(options);
907+
} catch (error) {
908+
console.error('Error occurred:', error);
909+
}
910+
});
911+
830912
// utility function used to remove items from an array that match 'item'
831913
const removeFromArray = (module.exports.removeFromArray = async (array, item) => {
832914
const index = array.indexOf(item);

Diff for: generator/templates/simple/src/App.vue

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<%_ if (rootOptions.router) { _%>
22
<%# -------------------- IS Using vue-router -------------------- -%>
3+
<%_ if (!options.isNativeOnly) { _%>
34
<template web>
45
<div class="w-page">
56
<nav>
@@ -15,6 +16,9 @@
1516
</div>
1617
</template>
1718
<template native>
19+
<%_ } else { _%>
20+
<template>
21+
<%_ } _%>
1822
<Page>
1923
<ActionBar :title="navbarTitle"/>
2024
<GridLayout rows="auto, auto">
@@ -25,6 +29,7 @@
2529
</template>
2630
<%_ } else { _%>
2731
<%# -------------------- IS NOT Using vue-router -------------------- -%>
32+
<%_ if (!options.isNativeOnly) { _%>
2833
<template web>
2934
<div class="w-page">
3035
<div class="w-container">
@@ -34,6 +39,9 @@
3439
</div>
3540
</template>
3641
<template native>
42+
<%_ } else { _%>
43+
<template>
44+
<%_ } _%>
3745
<Page>
3846
<ActionBar :title="navbarTitle"/>
3947
<GridLayout rows="auto, auto">

Diff for: generator/templates/simple/src/views/About.vue

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%_ if (!options.isNativeOnly) { _%>
12
<template web>
23
<div class="w-page">
34
<div class="w-container">
@@ -6,6 +7,9 @@
67
</div>
78
</template>
89
<template native>
10+
<%_ } else { _%>
11+
<template>
12+
<%_ } _%>
913
<Page>
1014
<ActionBar :title="navbarTitle"/>
1115
<GridLayout>

Diff for: generator/templates/simple/src/views/Home.vue

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%_ if (!options.isNativeOnly) { _%>
12
<template web>
23
<div class="w-page">
34
<div class="w-container">
@@ -7,6 +8,9 @@
78
</div>
89
</template>
910
<template native>
11+
<%_ } else { _%>
12+
<template>
13+
<%_ } _%>
1014
<Page>
1115
<ActionBar :title="navbarTitle"/>
1216
<GridLayout rows="auto, auto">

Diff for: generator/templates/vue-sfc-template.vue

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<%_ if (rootOptions.router) { _%>
22
<%# -------------------- IS Using vue-router -------------------- -%>
3+
<%_ if (!options.isNativeOnly) { _%>
34
<template web></template>
5+
<%_ } _%>
46
<template native></template>
57
<%_ } else { _%>
68
<%# -------------------- IS NOT Using vue-router -------------------- -%>
9+
<%_ if (!options.isNativeOnly) { _%>
710
<template web></template>
11+
<%_ } _%>
812
<template native></template>
913
<%_ } _%>
1014
<%_ if (!usingTS && rootOptions.router) { _%>

0 commit comments

Comments
 (0)