@@ -6,7 +6,7 @@ function usage {
66usage
77-----
88
9- [env TEMPLATE_PROJECT_DIR ="$(npm prefix)"] vue-cli-template-dev-server.sh [<output_dir> ] [<output_project_name >] [-h ]
9+ [env SOURCE_DIR ="$(npm prefix)" TARGET_INIT_COMMAND="dev" ] vue-cli-template-dev-server.sh [-h ] [<target-dir >] [<project-name> ]
1010
1111 ' '
1212'
@@ -17,30 +17,41 @@ function info {
1717overview
1818--------
1919
20- watch source files of a vue.js custom template project, and triggers the vue-cli init to rebuild the project.
20+ watches source files of a vue-cli custom template project, and triggers the vue-cli init command to rebuild the project.
2121 to be used while developing custom templates.
2222
2323
24- environment
25- -----------
24+ flags
25+ -----
2626
27- TEMPLATE_PROJECT_DIR
28- the local path of the vue-init custom template project to watch.
29- defaults to "$(npm prefix)".
27+ -h
28+ shows this help page, and quits.
3029
3130
32- arguments
33- ---------
31+ operands
32+ --------
3433
35- output_dir
36- a local path of the desired directory to pour the generated project into.
34+ target-dir
35+ a path, relative to the custom template project root, of a directory to pour the generated files into.
3736 the generated project will be created as a child directory of that path.
38- defaults to "${TEMPLATE_PROJECT_DIR }/out".
37+ defaults to "${SOURCE_DIR }/out".
3938
40- output_project_name
41- a name for the directory of the project that will be generated by vue init .
39+ project-name
40+ a name for the generated project directory, which will be nested inside the target directory .
4241 defaults to "awesome-vue-app".
4342
43+
44+ environment
45+ -----------
46+
47+ SOURCE_DIR
48+ the local path of the custom template project to watch.
49+ defaults to "$(npm prefix)" (the host NPM project).
50+
51+ TARGET_INIT_COMMAND
52+ the name of an NPM script command in the output project, to be run once it is generated, right after installing its dependencies.
53+ defaults to "dev".
54+
4455 ' '
4556'
4657}
@@ -59,10 +70,13 @@ function main {
5970function start {
6071 local temp_dir
6172 local expect_file
62- local output_dir=" ${1:- ${TEMPLATE_PROJECT_DIR} / out} "
63- local output_project_name=" ${2:- awesome-vue-app} "
73+ local target_dir=" ${1:- ${SOURCE_DIR} / out} "
74+ local target_project_name=" ${2:- awesome-vue-app} "
75+
76+ # we have to ensure the directory exists first, as abs_path() will fail on non-existing directories
77+ ensure_dir " $target_dir "
6478
65- output_dir =" $( abs_path " $output_dir " ) "
79+ target_dir =" $( abs_path " $target_dir " ) "
6680 temp_dir=" $( create_temp_dir) "
6781 expect_file=" ${temp_dir} /vue-init.exp"
6882
@@ -71,22 +85,26 @@ function start {
7185 log ' hi :)'
7286
7387 log ' starting vue init survey...'
74- start_auto_survey ${output_dir} ${expect_file} ${TEMPLATE_PROJECT_DIR} " ${output_project_name} "
75- log ' project generated using provided answers'
76- log ' waiting for changes...'
88+ start_auto_survey ${target_dir} ${expect_file} ${SOURCE_DIR} " ${target_project_name} "
89+ log ' output project generated using provided answers'
90+ log ' initializing output project...'
91+ initialize_target_project ${target_dir} /${target_project_name} " $TARGET_INIT_COMMAND "
92+ log ' output project initialized'
7793
78- fswatch -o " ${TEMPLATE_PROJECT_DIR} /template" | while read num; do
94+ log ' waiting for changes...'
95+ fswatch -o " ${SOURCE_DIR} /template" | while read num; do
7996 log ' change detected'
8097 log ' auto-generating output project...'
81- start_survey ${output_dir } ${expect_file}
82- log ' project regenerated using initial answers'
98+ start_survey ${target_dir } ${expect_file}
99+ log ' output project regenerated using initial answers'
83100 log ' waiting for changes...'
84101 done
85102}
86103
87104function setup_environment {
88- validate_template_project_dir
89- ensure_template_project_dir
105+ validate_source_dir
106+ ensure_source_dir
107+ ensure_target_init_command
90108}
91109
92110function ensure_dependencies {
@@ -97,16 +115,17 @@ function ensure_dependencies {
97115function start_auto_survey {
98116 local work_dir=" $1 "
99117 local expect_file=" $2 "
100- local template_project_dir =" $3 "
101- local output_project_name =" $4 "
118+ local source_dir =" $3 "
119+ local target_project_name =" $4 "
102120
103121 # create a directory to force vue-init to ask about overwriting the directory on the first time
104- ensure_dir " ${work_dir} /$output_project_name "
122+ ensure_dir " ${work_dir} /$target_project_name "
105123
106- pushd ${work_dir} > /dev/null 2>&1
107- # this will generate an expect file, all filled with answers to the vue-init survey questions
108- autoexpect -quiet -f ${expect_file} vue init ${template_project_dir} " ${output_project_name} "
109- popd > /dev/null 2>&1
124+ (
125+ cd ${work_dir} > /dev/null 2>&1
126+ # this will generate an expect file, all filled with answers to the vue-init survey questions
127+ autoexpect -quiet -f ${expect_file} vue init ${source_dir} " ${target_project_name} "
128+ )
110129
111130 search_line ${expect_file} ' expect -exact.*'
112131 if (( $? == 1 )) ; then
@@ -121,39 +140,69 @@ function start_auto_survey {
121140 chmod u+x ${expect_file}
122141}
123142
143+ function initialize_target_project {
144+ local project_dir=" $1 "
145+ local init_command=" $2 "
146+
147+ if ! is_npm_repo " $project_dir " ; then
148+ return 0
149+ fi
150+
151+ (
152+ cd ${project_dir} > /dev/null 2>&1
153+
154+ # install dependencies
155+ npm i > /dev/null 2>&1
156+ if (( $? == 1 )) ; then
157+ quit " failed installing the output project's npm dependencies"
158+ fi
159+
160+ # launch dev server / whatever
161+ if is_npm_command_available " $init_command " ; then
162+ npm run " $init_command " > /dev/null 2>&1
163+ if (( $? == 1 )) ; then
164+ quit " the output project's initialize command execution failed"
165+ fi
166+ fi
167+ )
168+ }
169+
124170function start_survey {
125171 local work_dir=" $1 "
126172 local expect_file=" $2 "
127173
128- pushd ${work_dir} > /dev/null 2>&1
129- ${expect_file} > /dev/null 2>&1
130- popd > /dev/null 2>&1
174+ (
175+ cd ${work_dir} > /dev/null 2>&1
176+ ${expect_file} > /dev/null 2>&1
177+ )
131178}
132179
133- function validate_template_project_dir {
134- if [[ -n " $TEMPLATE_PROJECT_DIR " ]]; then
135- if [[ ! -d " $TEMPLATE_PROJECT_DIR " ]]; then
136- quit ' $TEMPLATE_PROJECT_DIR is not a directory'
180+ function validate_source_dir {
181+ if [[ -n " $SOURCE_DIR " ]]; then
182+ if [[ ! -d " $SOURCE_DIR " ]]; then
183+ quit ' $SOURCE_DIR is not a directory'
137184 fi
138- if ! is_npm_repo " ${TEMPLATE_PROJECT_DIR } " ; then
139- quit ' $TEMPLATE_PROJECT_DIR is not an npm project'
185+ if ! is_npm_repo " ${SOURCE_DIR } " ; then
186+ quit ' $SOURCE_DIR is not an npm project'
140187 fi
141188 else
142- # we know that npm_host_path will be the default value in case
143- # $TEMPLATE_PROJECT_DIR is empty.
189+ # we know that npm_host_path will be the default value in case $SOURCE_DIR is empty.
144190 if [[ -z " $( npm_host_path) " ]]; then
145191 quit ' could not resolve npm host'
146192 fi
147193 fi
148194}
149195
150- function ensure_template_project_dir {
151- TEMPLATE_PROJECT_DIR=" ${TEMPLATE_PROJECT_DIR:- $(npm_host_path)} "
152- TEMPLATE_PROJECT_DIR=" ${TEMPLATE_PROJECT_DIR: +$(abs_path " $TEMPLATE_PROJECT_DIR " )} "
196+ function ensure_source_dir {
197+ SOURCE_DIR=" ${SOURCE_DIR:- $(npm_host_path)} "
198+ SOURCE_DIR=" ${SOURCE_DIR: +$(abs_path " $SOURCE_DIR " )} "
199+ }
200+
201+ function ensure_target_init_command {
202+ TARGET_INIT_COMMAND=" ${TARGET_INIT_COMMAND:- dev} "
153203}
154204
155205function npm_host_path {
156- local path
157206 local npm_prefix=" $( npm prefix) "
158207 if is_npm_repo " $npm_prefix " ; then
159208 printf " %s" " $npm_prefix "
@@ -164,25 +213,44 @@ function npm_host_path {
164213
165214function is_npm_repo {
166215 local dir=" $1 "
167- if [[ -f " ${dir} /package.json" ]]; then
168- return 0
169- else
170- return 1
171- fi
216+ is_file " ${dir} /package.json"
217+ }
218+
219+ function is_npm_command_available {
220+ local cmd=" $1 "
221+ [[ -n " $cmd " && -n " $( npm run --parseable | grep ' ^' " $cmd " ' :' ) " ]]
172222}
173223
174224function abs_path {
175225 local path=" $1 "
226+ if ! path_exist " $path " ; then
227+ return 1
228+ fi
176229 printf " %s" " $( cd " $( dirname " $path " ) " ; pwd) /$( basename " $path " ) "
177230}
178231
179232function ensure_dir {
180- local dir =" $1 "
181- if [[ ! -d " $dir " ]] ; then
182- mkdir -p " $dir "
233+ local path =" $1 "
234+ if ! is_dir " $path " ; then
235+ mkdir -p " $path "
183236 fi
184237}
185238
239+ function is_file {
240+ local path=" $1 "
241+ [[ -n " $path " && -f " $path " ]]
242+ }
243+
244+ function is_dir {
245+ local path=" $1 "
246+ [[ -n " $path " && -d " $path " ]]
247+ }
248+
249+ function path_exist {
250+ local path=" $1 "
251+ [[ -n " $path " && -e " $path " ]]
252+ }
253+
186254function remove_dir {
187255 rm -rf " $@ " > /dev/null 2>&1
188256}
0 commit comments