Unverified Commit 5c00455f authored by René Dudfield's avatar René Dudfield Committed by GitHub
Browse files

Add optional folder support to headlamp-plugin build command (#235)


So that the build command can build a package or folder of
packages given.

Stop printing the watch message for building.
Stop copying the built plugins to the config/plugins folder for
production builds.
Co-authored-by: default avatarJoaquim Rocha <joaquim@kinvolk.io>
parent 4bb95490
Showing with 77 additions and 11 deletions
+77 -11
......@@ -39,6 +39,16 @@ npm run build
This will create a file with the bundled plugin in
`headlamp-myfancy/dist/main.js`.
### Building a folder of packages at once
For convienience the `headlamp-plugin build` command can build a
package or folder of packages.
```bash
npx @kinvolk/headlamp-plugin build myplugins/headlamp-myfancy
npx @kinvolk/headlamp-plugin build myplugins
```
## Shipping / Deploying Plugins
Once a plugin is ready to be shipped (built for production) it needs to
......
......@@ -20,6 +20,6 @@
"directory": "plugins/headlamp-plugin-counter"
},
"devDependencies": {
"@kinvolk/headlamp-plugin": "^0.2.0"
"@kinvolk/headlamp-plugin": "^0.3.0"
}
}
......@@ -12,7 +12,9 @@ This package is published to the npm package index separately from Headlamp.
```
headlamp-plugin --help
headlamp-plugin build Build the plugin
headlamp-plugin build <package> Build the plugin, or folder of
plugins. <package> defaults to
current working directory.
headlamp-plugin start Watch for changes and build the plugin
headlamp-plugin create <name> Create a new plugin folder with base code
headlamp-plugin extract Copies folders of packages from plug
......
......@@ -211,20 +211,74 @@ function start() {
}
/**
* Build the plugin for production.
* Build the plugin package or folder of packages for production.
*
* @param packageFolder {string} - folder where the package, or folder of packages is.
*/
function build() {
console.log('Watching for changes to plugin...');
function build(packageFolder) {
if (!fs.existsSync(packageFolder)) {
console.error(`"${packageFolder}" does not exist. Not building.`);
return 1;
}
const oldCwd = process.cwd();
const oldBabelEnv = process.env['BABEL_ENV'];
process.env['BABEL_ENV'] = 'production';
copyToPluginsFolder(config);
webpack(config, compile);
function buildPackage(folder) {
if (!fs.existsSync(path.join(folder, 'package.json'))) {
return false;
}
process.chdir(folder);
console.log(`Building "${folder}" for production...`);
webpack(config, compile);
console.log(`Done building: "${folder}".`);
process.chdir(oldCwd);
return true;
}
function buildFolderOfPackages() {
const folders = fs.readdirSync(packageFolder, { withFileTypes: true })
.filter(fileName => {
return (
fileName.isDirectory() &&
fs.existsSync(path.join(packageFolder, fileName.name, 'package.json'))
);
});
folders.forEach((folder) => {
const folderToBuild = path.join(packageFolder, folder.name);
if (!buildPackage(folderToBuild)) {
console.error(`"${folderToBuild}" does not contain a package. Not building.`);
}
});
return folders.length !== 0;
}
if (!(buildPackage(packageFolder) || buildFolderOfPackages())) {
console.error(`"${packageFolder}" does not contain a package or packages. Not building.`);
};
process.env['BABEL_ENV'] = oldBabelEnv;
return 0;
}
const argv = yargs(process.argv.slice(2))
.command('build', 'Build the plugin', {}, (argv) => {
process.exitCode = build();
.command('build <package>', (
'Build the plugin, or folder of plugins. ' +
'<package> defaults to current working directory.'
),
(yargs) => {
yargs.positional('package', {
describe: 'Package or folder of packages to build',
type: 'string',
default: '.',
})
}, (argv) => {
process.exitCode = build(argv.package);
})
.command('start', 'Watch for changes and build plugin', {}, (argv) => {
process.exitCode = start();
......
{
"name": "@kinvolk/headlamp-plugin",
"version": "0.2.0",
"version": "0.3.0",
"description": "The needed infrastructure for building Headlamp plugins.",
"bin": "bin/headlamp-plugin.js",
"dependencies": {
......
......@@ -13,6 +13,6 @@
"plugins"
],
"devDependencies": {
"@kinvolk/headlamp-plugin": "^0.2.0"
"@kinvolk/headlamp-plugin": "^0.3.0"
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment