From 62fbf453dd3ee5c5a1b1679f550b7b144b1e5234 Mon Sep 17 00:00:00 2001 From: "xiaowu.yan" Date: Thu, 9 Mar 2017 13:26:31 +0800 Subject: [PATCH 1/5] If no offline specified. We should remove the tmp dir if it is existed. --- bin/vue-init | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/vue-init b/bin/vue-init index d9b85c17cc..c69ca9557d 100755 --- a/bin/vue-init +++ b/bin/vue-init @@ -14,6 +14,7 @@ var generate = require('../lib/generate') var checkVersion = require('../lib/check-version') var warnings = require('../lib/warnings') var { isLocalPath, getTemplatePath } = require('../lib/local-path') +var rm = require('rimraf').sync /** * Usage. @@ -62,9 +63,11 @@ var to = path.resolve(rawName || '.') var clone = program.clone || false var tmp = path.join(home, '.vue-templates', template.replace(/\//g, '-')) -if (program.offline) { +if (program.offline && exists(tmp)) { console.log(`> Use cached template at ${chalk.yellow(tildify(tmp))}`) template = tmp +} else { + rm(tmp) } /** From 8aa9c177aafb808c04e7874a76309c02fd0261af Mon Sep 17 00:00:00 2001 From: "xiaowu.yan" Date: Thu, 9 Mar 2017 14:17:34 +0800 Subject: [PATCH 2/5] Separate the downloads an offline cache. --- bin/vue-init | 14 +++++++++++--- package.json | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bin/vue-init b/bin/vue-init index c69ca9557d..677432fe26 100755 --- a/bin/vue-init +++ b/bin/vue-init @@ -15,6 +15,7 @@ var checkVersion = require('../lib/check-version') var warnings = require('../lib/warnings') var { isLocalPath, getTemplatePath } = require('../lib/local-path') var rm = require('rimraf').sync +var ncp = require('ncp').ncp; /** * Usage. @@ -66,8 +67,6 @@ var tmp = path.join(home, '.vue-templates', template.replace(/\//g, '-')) if (program.offline && exists(tmp)) { console.log(`> Use cached template at ${chalk.yellow(tildify(tmp))}`) template = tmp -} else { - rm(tmp) } /** @@ -142,9 +141,13 @@ function run () { */ function downloadAndGenerate (template) { + var tmpCache = tmp + '_cache' + if (exists(tmpCache)) { + rm(tmpCache) + } var spinner = ora('downloading template') spinner.start() - download(template, tmp, { clone: clone }, function (err) { + download(template, tmpCache, { clone: clone }, function (err) { spinner.stop() if (err) logger.fatal('Failed to download repo ' + template + ': ' + err.message.trim()) generate(name, tmp, to, function (err) { @@ -152,5 +155,10 @@ function downloadAndGenerate (template) { console.log() logger.success('Generated "%s".', name) }) + ncp(tmpCache, tmp, function (err) { + if (err) { + logger.log('Failed to cache template!') + } + }); }) } diff --git a/package.json b/package.json index d8d89e46b8..d8cfe86a53 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "metalsmith": "^2.1.0", "minimatch": "^3.0.0", "multimatch": "^2.1.0", + "ncp": "^2.0.0", "opn": "^4.0.2", "ora": "^0.2.1", "post-compile-webpack-plugin": "^0.1.0", From cdf7585ce1f63724d62cfa4208bd312401f2274b Mon Sep 17 00:00:00 2001 From: "xiaowu.yan" Date: Thu, 9 Mar 2017 14:37:10 +0800 Subject: [PATCH 3/5] Remove tmp cache dir. --- bin/vue-init | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bin/vue-init b/bin/vue-init index 677432fe26..b31ba80958 100755 --- a/bin/vue-init +++ b/bin/vue-init @@ -150,15 +150,19 @@ function downloadAndGenerate (template) { download(template, tmpCache, { clone: clone }, function (err) { spinner.stop() if (err) logger.fatal('Failed to download repo ' + template + ': ' + err.message.trim()) - generate(name, tmp, to, function (err) { + generate(name, tmpCache, to, function (err) { if (err) logger.fatal(err) console.log() logger.success('Generated "%s".', name) + ncp(tmpCache, tmp, function (err) { + if (err) { + logger.log('Failed to cache template!') + rm(tmpCache) + rm(tmp) + } else { + rm(tmpCache) + } + }) }) - ncp(tmpCache, tmp, function (err) { - if (err) { - logger.log('Failed to cache template!') - } - }); }) } From 05e0907aa7c4d4c1ee5e68b3ecb4b83394f5401e Mon Sep 17 00:00:00 2001 From: "xiaowu.yan" Date: Thu, 9 Mar 2017 14:39:39 +0800 Subject: [PATCH 4/5] Remove the tmp cache dir. --- bin/vue-init | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bin/vue-init b/bin/vue-init index b31ba80958..651e88e3a8 100755 --- a/bin/vue-init +++ b/bin/vue-init @@ -155,12 +155,9 @@ function downloadAndGenerate (template) { console.log() logger.success('Generated "%s".', name) ncp(tmpCache, tmp, function (err) { + rm(tmpCache) if (err) { - logger.log('Failed to cache template!') - rm(tmpCache) rm(tmp) - } else { - rm(tmpCache) } }) }) From 8a68d2828a2fc144e41f8470273f8800d158d6f7 Mon Sep 17 00:00:00 2001 From: "xiaowu.yan" Date: Thu, 9 Mar 2017 14:47:18 +0800 Subject: [PATCH 5/5] Fix eslint error. --- bin/vue-init | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/vue-init b/bin/vue-init index 651e88e3a8..3c418e43e8 100755 --- a/bin/vue-init +++ b/bin/vue-init @@ -15,7 +15,7 @@ var checkVersion = require('../lib/check-version') var warnings = require('../lib/warnings') var { isLocalPath, getTemplatePath } = require('../lib/local-path') var rm = require('rimraf').sync -var ncp = require('ncp').ncp; +var ncp = require('ncp').ncp /** * Usage.