diff --git a/lib/commands/deploy.js b/lib/commands/deploy.js index 9780d9863..846590285 100644 --- a/lib/commands/deploy.js +++ b/lib/commands/deploy.js @@ -1,32 +1,8 @@ 'use strict'; -const fs = require('fs-extra'); -const path = require('path'); const { red } = require('colors'); -const { readJsonFromFile } = require('../utils/file'); -const { getRootTplPath, detectTplPath, validateTplName, DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX} = require('../tpl'); - -const _ = require('lodash'); - -async function getModifiedTimes(tplPath) { - if (tplPath.indexOf(DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX) === -1) { return {}; } - - const rootTplPath = getRootTplPath(tplPath); - const metaPath = path.resolve(path.dirname(rootTplPath), DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX, 'meta.json'); - - if (!await fs.pathExists(metaPath)) { return {}; } - - const metaObj = await readJsonFromFile(metaPath); - - if (_.isEmpty(metaObj)) { - return {}; - } - - return _.pickBy((metaObj.modifiedTimes || {}), (mtime, filePath) => { - const lstat = fs.lstatSync(filePath); - return mtime !== lstat.mtime.getTime().toString(); - }); -} +const { ensureFilesModified } = require('../utils/file'); +const { detectTplPath, validateTplName } = require('../tpl'); async function deploy(context) { let tplPath = context.template; @@ -41,14 +17,7 @@ async function deploy(context) { validateTplName(tplPath); - const modifiedTimes = await getModifiedTimes(tplPath); // for fun build scene - - if (!_.isEmpty(modifiedTimes)) { - throw new Error(` - ${Object.keys(modifiedTimes).join('\n\t')}\n` + -` -Fun detected the above path have been modified. Please execute ‘fun build’ to compile your functions.`); - } + await ensureFilesModified(tplPath); await require('../deploy/deploy-by-tpl').deploy(tplPath, context); } diff --git a/lib/commands/local/invoke.js b/lib/commands/local/invoke.js index fd6a68fa9..9d5207cdc 100644 --- a/lib/commands/local/invoke.js +++ b/lib/commands/local/invoke.js @@ -5,10 +5,10 @@ const path = require('path'); const debug = require('debug')('fun:local'); const definition = require('../../definition'); -const { getEvent } = require('../../utils/file'); const { red, yellow } = require('colors'); const { ensureTmpDir } = require('../../utils/path'); const { getDebugPort, getDebugIde } = require('../../debug'); +const { getEvent, ensureFilesModified } = require('../../utils/file'); const { generateMergedTpl, detectNasBaseDir } = require('../../tpl'); async function localInvoke(invokeName, tplPath, tpl, debugPort, event, debugIde, baseDir, tmpDir, debuggerPath, debugArgs, reuse = true, nasBaseDir) { @@ -47,7 +47,7 @@ async function invoke(invokeName, options) { const event = await getEvent(options.event); debug('event content: ' + event); - + const debugPort = getDebugPort(options); const debugIde = getDebugIde(options); @@ -56,9 +56,11 @@ async function invoke(invokeName, options) { const baseDir = path.resolve(path.dirname(tplPath)); const reuse = options.reuse; - + const nasBaseDir = detectNasBaseDir(tplPath); + await ensureFilesModified(tplPath); + await localInvoke(invokeName, tplPath, tpl, debugPort, event, debugIde, baseDir, options.tmpDir, debuggerPath, debugArgs, reuse, nasBaseDir); } diff --git a/lib/commands/local/start.js b/lib/commands/local/start.js index 32a4ab1f3..ba91b2c70 100644 --- a/lib/commands/local/start.js +++ b/lib/commands/local/start.js @@ -10,6 +10,7 @@ const httpSupport = require('./http-support'); const EventStart = require('../../local/event-start'); const { ensureTmpDir } = require('../../utils/path'); +const { ensureFilesModified } = require('../../utils/file'); const { showLocalStartNextTips } = require('../../../lib/build/tips'); const { promptForDebugaHttptriggers } = require('../../../lib/init/prompt'); const { getDebugPort, getDebugIde } = require('../../debug'); @@ -168,6 +169,8 @@ async function start(options, invokeName = '') { const debugArgs = options.debugArgs; const baseDir = path.dirname(tplPath); + await ensureFilesModified(tplPath); + let httpTriggers = definition.findHttpTriggersInTpl(tpl); const [domainName, routePath] = definition.parseDomainRoutePath(invokeName); diff --git a/lib/package/package.js b/lib/package/package.js index c6de7df40..8d5179bce 100644 --- a/lib/package/package.js +++ b/lib/package/package.js @@ -1,5 +1,6 @@ 'use strict'; +const { ensureFilesModified } = require('../utils/file'); const { isEmptyDir } = require('../nas/cp/file'); const { getOssClient } = require('../client'); const { parseMountDirPrefix } = require('../fc'); @@ -168,6 +169,9 @@ async function pack(tplPath, bucket, outputTemplateFile) { if (!bucket) { throw new Error('Missing OSS bucket'); } + + await ensureFilesModified(tplPath); + const ossClient = await getOssClient(bucket); const updatedEnvTpl = await processNasPythonPaths(tpl, tplPath); diff --git a/lib/utils/file.js b/lib/utils/file.js index 5774286a8..25523ee47 100644 --- a/lib/utils/file.js +++ b/lib/utils/file.js @@ -1,10 +1,14 @@ 'use strict'; const fs = require('fs-extra'); +const path = require('path'); const readline = require('readline'); const getStdin = require('get-stdin'); const { red } = require('colors'); +const { getRootTplPath, DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX } = require('../tpl'); + +const _ = require('lodash'); const getVisitor = require('../visitor').getVisitor; @@ -64,13 +68,13 @@ async function getEvent(eventFile, ec = 'local invoke', dp = '/fun/local/invoke' el: 'success', dp }).send(); - + resolve(event); }); }); - + rl.on('SIGINT', function () { - + getVisitor().then(visitor => { visitor.event({ ec, @@ -78,7 +82,6 @@ async function getEvent(eventFile, ec = 'local invoke', dp = '/fun/local/invoke' el: 'cancel', dp }).send(); - // Keep the behavior consistent with system. reject(new Error('^C')); }); @@ -105,7 +108,7 @@ async function recordMtimes(filePaths, buildOps, recordedPath) { const modifiedTimeObj = collection.modifiedTimes || {}; Object.assign(collection, { - 'modifiedTimes': Object.assign(modifiedTimeObj, { + 'modifiedTimestamps': Object.assign(modifiedTimeObj, { [cur]: lstat.mtime.getTime().toString() }) }); @@ -132,4 +135,36 @@ async function readJsonFromFile(absFilePath) { return obj; } -module.exports = { readLines, getEvent, recordMtimes, readJsonFromFile }; \ No newline at end of file +async function getModifiedTimestamps(tplPath) { + if (tplPath.indexOf(DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX) === -1) { return {}; } + + const rootTplPath = getRootTplPath(tplPath); + const metaPath = path.resolve(path.dirname(rootTplPath), DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX, 'meta.json'); + + if (!await fs.pathExists(metaPath)) { return {}; } + + const metaObj = await readJsonFromFile(metaPath); + + if (_.isEmpty(metaObj)) { + return {}; + } + + return _.pickBy((metaObj.modifiedTimestamps || {}), (mtime, filePath) => { + const lstat = fs.lstatSync(filePath); + return mtime !== lstat.mtime.getTime().toString(); + }); +} + +// for fun build scene +async function ensureFilesModified(tplPath) { + const modifiedTimes = await getModifiedTimestamps(tplPath); + + if (!_.isEmpty(modifiedTimes)) { + throw new Error(` + ${Object.keys(modifiedTimes).join('\n\t')}\n` + +` +Fun detected the above path have been modified. Please execute ‘fun build’ to compile your functions.`); + } +} + +module.exports = { readLines, getEvent, recordMtimes, readJsonFromFile, ensureFilesModified }; \ No newline at end of file