-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STRWEB-113 expose 'build' command publicly
**DRAFT** **DRAFT** **DRAFT** Expose `build` as a `bin` script, allowing a platform to directly depend on this module and call the `build` API to generate a bundle without pulling in all the stripes-cli deps that are unrelated to producing a production bundle. CSS isn't being correctly bundled/handled here, but the bundle is otherwise functional. Hopefully, this is just a config glitch. Refs STRWEB-113
- Loading branch information
Showing
7 changed files
with
404 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const Configstore = require('configstore'); | ||
|
||
// TODO: May want to modify storage key if running CLI locally | ||
const storageKey = '@folio/stripes-cli'; | ||
|
||
const storageDefault = { | ||
platforms: {}, | ||
}; | ||
|
||
const platformDefault = { | ||
aliases: {}, | ||
}; | ||
|
||
// Creates and persists a virtual platform for use by the CLI | ||
// Currently this maintains aliases for mapping a virtual platform during build | ||
// TODO: This could also manage stripes.config.js properties like okapi, config, and modules | ||
module.exports = class PlatformStorage { | ||
constructor(stripesConfig, platformName) { | ||
this.platformKey = platformName || 'default'; | ||
this.config = new Configstore(storageKey, storageDefault); | ||
|
||
// Initialize platform storage | ||
if (!this.config.has(`platforms.${this.platformKey}`)) { | ||
this.config.set(`platforms.${this.platformKey}`, platformDefault); | ||
} | ||
} | ||
|
||
aliasKey(moduleName) { | ||
if (moduleName) { | ||
return `platforms.${this.platformKey}.aliases.${moduleName}`; | ||
} | ||
return `platforms.${this.platformKey}.aliases`; | ||
} | ||
|
||
addAlias(moduleName, absolutePath) { | ||
const key = this.aliasKey(moduleName); | ||
this.config.set(key, absolutePath); // store absolute path | ||
return true; | ||
} | ||
|
||
hasAlias(moduleName) { | ||
const key = this.aliasKey(moduleName); | ||
return this.config.has(key); | ||
} | ||
|
||
removeAlias(moduleName) { | ||
const key = this.aliasKey(moduleName); | ||
if (this.config.has(key)) { | ||
this.config.delete(key); | ||
} | ||
} | ||
|
||
clearAliases() { | ||
return this.config.set(this.aliasKey(), {}); | ||
} | ||
|
||
getAllAliases() { | ||
return this.config.get(this.aliasKey()); | ||
} | ||
|
||
getStoragePath() { | ||
return this.config.path; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const build = require('../webpack/build'); | ||
const nodeApi = require('../webpack/stripes-node-api'); | ||
const StripesPlatform = require('./stripes-platform'); | ||
|
||
nodeApi.StripesModuleParser = require('../webpack/stripes-module-parser').StripesModuleParser; | ||
nodeApi.StripesBuildError = require('../webpack/stripes-build-error'); | ||
|
||
function processStats(stats) { | ||
console.log(stats.toString({ | ||
chunks: false, | ||
colors: true, | ||
})); | ||
// Check for webpack compile errors and exit | ||
if (stats.hasErrors()) { | ||
processError(); | ||
} | ||
} | ||
|
||
process.title = 'stripes-cli'; | ||
process.env.NODE_ENV = process.env.NODE_ENV ? process.env.NODE_ENV : 'production'; | ||
|
||
const stripesConfig = require(path.join(process.env.PWD, process.argv[3])); | ||
const output = process.argv.length === 5 ? process.argv[4] : null; | ||
|
||
const argv = { | ||
stripesConfig, | ||
output, | ||
}; | ||
const context = {}; | ||
|
||
const platform = new StripesPlatform(argv.stripesConfig, context, argv); | ||
const webpackOverrides = platform.getWebpackOverrides(context); | ||
|
||
if (argv.output) { | ||
argv.outputPath = argv.output; | ||
} else if (!argv.outputPath) { | ||
argv.outputPath = './output'; | ||
} | ||
if (argv.maxChunks) { | ||
webpackOverrides.push(limitChunks(argv.maxChunks)); | ||
} | ||
if (argv.cache === false) { | ||
webpackOverrides.push(ignoreCache); | ||
} | ||
if (context.plugin && context.plugin.beforeBuild) { | ||
webpackOverrides.push(context.plugin.beforeBuild(argv)); | ||
} | ||
|
||
console.log('Building...'); | ||
nodeApi.build(platform.getStripesConfig(), Object.assign({}, argv, { webpackOverrides })) | ||
.then(processStats) | ||
.catch(e => { | ||
console.error('ERROR', e); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const path = require('path'); | ||
const { defaultConfig, emptyConfig, mergeConfig } = require('./tenant-config'); | ||
const webpackCommon = require('./webpack-common'); | ||
const logger = console; | ||
|
||
module.exports = class StripesPlatform { | ||
constructor(stripesConfig, context, options) { | ||
this.isAppContext = false; | ||
this.aliases = {}; | ||
this.addAliasesAsModules = true; | ||
|
||
// Start with stripes.config.js or internal defaults | ||
this.applyDefaultConfig(stripesConfig); | ||
|
||
// Apply any command options last | ||
this.applyCommandOptions(options); | ||
} | ||
|
||
applyDefaultConfig(stripesConfig) { | ||
// TODO: Validate incoming config | ||
if (stripesConfig) { | ||
// When modules are specified in a config file, do not automatically apply aliases as modules | ||
if (stripesConfig.modules) { | ||
this.addAliasesAsModules = false; | ||
} | ||
this.config = mergeConfig(emptyConfig, stripesConfig); | ||
} else { | ||
this.config = mergeConfig(emptyConfig, defaultConfig); | ||
} | ||
} | ||
|
||
applyCommandOptions(options) { | ||
if (options) { | ||
if (options.okapi) { | ||
this.config.okapi.url = options.okapi; | ||
} | ||
if (options.tenant) { | ||
this.config.okapi.tenant = options.tenant; | ||
} | ||
if (options.hasAllPerms) { | ||
this.config.config.hasAllPerms = true; | ||
} | ||
if (options.languages) { | ||
this.config.config.languages = options.languages; | ||
} | ||
} | ||
} | ||
|
||
getWebpackOverrides(context) { | ||
const overrides = []; | ||
overrides.push(webpackCommon.cliResolve(context)); | ||
overrides.push(webpackCommon.cliAliases(this.aliases)); | ||
return overrides; | ||
} | ||
|
||
getStripesConfig() { | ||
const config = Object.assign({}, this.config); | ||
delete config.aliases; | ||
logger.log('using stripes tenant config:', config); | ||
return config; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { merge } = require('lodash'); | ||
|
||
const defaultConfig = { | ||
okapi: { | ||
url: 'http://localhost:9130', | ||
tenant: 'diku', | ||
}, | ||
config: { | ||
logCategories: 'core,path,action,xhr', | ||
logPrefix: '--', | ||
showPerms: false, | ||
hasAllPerms: false, | ||
languages: ['en'], | ||
useSecureTokens: true, | ||
}, | ||
modules: { | ||
}, | ||
branding: { | ||
}, | ||
}; | ||
|
||
const emptyConfig = { | ||
okapi: {}, | ||
config: {}, | ||
modules: {}, | ||
branding: {}, | ||
}; | ||
|
||
// Merge two stripes configurations | ||
function mergeConfig(base, extend) { | ||
return merge({}, base, extend); | ||
} | ||
|
||
module.exports = { | ||
defaultConfig, | ||
emptyConfig, | ||
mergeConfig, | ||
}; |
Oops, something went wrong.