mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
Fix #5242 Do this by first supplying the `servicePath` in `~/lib/plugins/lib/validate.js` and `~/lib/plugins/print.js` in a consistent manner with the code in `~/lib/classes/PluginManager.js`. Then, provide a default of `process.cwd()` for `servicePath` in `getCacheFilePath` and `getServerlessConfigFile`.
62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const path = require('path');
|
|
|
|
const Serverless = require('../Serverless');
|
|
const crypto = require('crypto');
|
|
|
|
const getCacheFile = require('./getCacheFile');
|
|
const getServerlessConfigFile = require('./getServerlessConfigFile');
|
|
|
|
const name = path.basename(process.argv[0]);
|
|
|
|
const tab = require('tabtab')({ name });
|
|
|
|
const getSuggestions = (commands) => {
|
|
tab.on(name, (data, done) => {
|
|
if (data.words === 1) {
|
|
done(null, Object.keys(commands));
|
|
} else {
|
|
done(null, []);
|
|
}
|
|
});
|
|
|
|
Object.keys(commands).forEach(command => {
|
|
tab.on(command, (data, done) => {
|
|
done(null, commands[command]);
|
|
});
|
|
});
|
|
|
|
tab.start();
|
|
};
|
|
|
|
const cacheFileValid = (serverlessConfigFile, validationHash) => {
|
|
const serverlessConfigFileHash = crypto.createHash('sha256')
|
|
.update(JSON.stringify(serverlessConfigFile)).digest('hex');
|
|
if (validationHash === serverlessConfigFileHash) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
const autocomplete = () => {
|
|
const servicePath = process.cwd();
|
|
return getServerlessConfigFile(servicePath)
|
|
.then((serverlessConfigFile) => getCacheFile(servicePath)
|
|
.then((cacheFile) => {
|
|
if (!cacheFile || !cacheFileValid(serverlessConfigFile, cacheFile.validationHash)) {
|
|
const serverless = new Serverless();
|
|
return serverless.init().then(() => getCacheFile(servicePath));
|
|
}
|
|
return cacheFile;
|
|
})
|
|
.then((cacheFile) => {
|
|
if (!cacheFile || !cacheFileValid(serverlessConfigFile, cacheFile.validationHash)) {
|
|
return;
|
|
}
|
|
return getSuggestions(cacheFile.commands); // eslint-disable-line consistent-return
|
|
}));
|
|
};
|
|
|
|
module.exports = autocomplete;
|