Skip to content

Commit

Permalink
support meta.json for fun local and fun package (alibaba#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
coco-super committed Feb 7, 2020
1 parent 56f3d68 commit 9a22900
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
37 changes: 3 additions & 34 deletions 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;
Expand All @@ -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);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/commands/local/invoke.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/commands/local/start.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions 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');
Expand Down Expand Up @@ -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);
Expand Down
47 changes: 41 additions & 6 deletions 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;

Expand Down Expand Up @@ -64,21 +68,20 @@ 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,
ea: 'getEvent',
el: 'cancel',
dp
}).send();

// Keep the behavior consistent with system.
reject(new Error('^C'));
});
Expand All @@ -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()
})
});
Expand All @@ -132,4 +135,36 @@ async function readJsonFromFile(absFilePath) {
return obj;
}

module.exports = { readLines, getEvent, recordMtimes, readJsonFromFile };
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 };

0 comments on commit 9a22900

Please sign in to comment.