Skip to content

Commit

Permalink
fix: correctly parse base config, simplify logic, only rm dir on build
Browse files Browse the repository at this point in the history
  • Loading branch information
amcgee committed Jul 23, 2021
1 parent bcc3ae8 commit 9891ea3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 43 deletions.
1 change: 1 addition & 0 deletions cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const handler = async ({
if (packAppOutput) {
const bundle = path.parse(paths.buildAppBundle)

await fs.remove(paths.buildAppBundleOutput)
// update bundle archive
await pack({
destination: path.resolve(cwd, bundle.dir),
Expand Down
92 changes: 52 additions & 40 deletions cli/src/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const { reporter, chalk, exit } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')
const finalArchivePath = require('../lib/finalArchivePath.js')
const makeBundle = require('../lib/makeBundle.js')
const parseConfig = require('../lib/parseConfig.js')
const makePaths = require('../lib/paths.js')

exports.command = 'pack [source]'

exports.describe = 'Create archive from the build.'
exports.describe = 'Create a .zip archive of a built application'

exports.builder = yargs =>
yargs
Expand All @@ -29,13 +30,13 @@ exports.builder = yargs =>
})
.option('app-name', {
type: 'string',
describe: 'The name of the application to replace in filename',
defaultDescription: 'config.name',
describe: 'The name of the app to replace in filename',
defaultDescription: '${config.name}',
})
.option('version', {
.option('app-version', {
type: 'string',
describe: 'The version of the application to replace in filename',
defaultDescription: 'config.version',
describe: 'The version of the app to replace in filename',
defaultDescription: '${config.version}',
})

exports.handler = async argv => {
Expand All @@ -45,42 +46,16 @@ exports.handler = async argv => {
destination,
filename,
appName,
version,
appVersion,
} = argv

const paths = makePaths(cwd)
const config = fs.readJsonSync(path.join(source, 'd2.config.json'), {
throws: false,
})
const isPlatformApp = fs.existsSync(paths.config)

if (!config) {
// we may be dealing with a library or a unbuilt app
// load the d2.config.js file from the project and check
if (isPlatformApp) {
const baseConfig = require(paths.config)

if (baseConfig.type !== 'app') {
exit(
1,
`Unsupported type '${baseConfig.type}', only 'app' is currently supported.`
)
}
}
if (filename && !filename.endsWith('.zip')) {
exit(1, `Output filename must have the extension .zip`)
}

const inputPath = path.resolve(cwd, source ? source : paths.buildAppOutput)

const outputPath = path.resolve(
cwd,
destination
? destination
: isPlatformApp
? paths.buildAppBundleOutput
: cwd,
filename ? filename : paths.buildAppBundleFile
)
const paths = makePaths(cwd)

const inputPath = path.resolve(cwd, source || paths.buildAppOutput)
if (!fs.existsSync(inputPath)) {
exit(
1,
Expand All @@ -91,10 +66,48 @@ exports.handler = async argv => {
)
}

const builtConfig = fs.readJsonSync(
path.join(inputPath, 'd2.config.json'),
{
throws: false,
}
)
const baseConfig = fs.existsSync(paths.config) ? parseConfig(paths) : null

const resolved = {
destination,
appName,
appVersion,
}
if (builtConfig) {
resolved.appName = resolved.appName || builtConfig.name
resolved.appVersion = resolved.appVersion || builtConfig.version
} else if (baseConfig) {
// we may be dealing with a library or a unbuilt app
// load the d2.config.js file from the project and check
if (baseConfig.type !== 'app') {
exit(
1,
`Unsupported type '${baseConfig.type}', only 'app' is currently supported.`
)
}

resolved.destination =
resolved.destination || paths.buildAppBundleOutput
resolved.appName = resolved.appName || baseConfig.name
resolved.appVersion = resolved.appVersion || baseConfig.version
}

const outputPath = path.resolve(
cwd,
resolved.destination || '.',
filename || paths.buildAppBundleFile
)

const archivePath = finalArchivePath({
filepath: outputPath,
name: appName || config ? config.name : 'app',
version: version || config ? config.version : 'latest',
name: resolved.appName || 'app',
version: resolved.appVersion || 'latest',
})

const logPath = path.relative(process.cwd(), inputPath)
Expand All @@ -104,6 +117,5 @@ exports.handler = async argv => {
)} at ${chalk.bold(path.relative(process.cwd(), archivePath))}...`
)

await fs.remove(paths.buildAppBundleOutput)
await makeBundle(inputPath, archivePath)
}
11 changes: 8 additions & 3 deletions docs/scripts/pack.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# d2 app scripts pack

Create a .zip archive of a built application
Create a .zip archive of a built application.

> **NOTE**: This command is currently unsupported for libraries
```sh
> d2 app scripts pack --help
d2-app-scripts pack [source]

Create archive from the build.
Create a .zip archive of a built application

Positionals:
source The source directory to pack relative to cwd.
Expand All @@ -24,7 +24,12 @@ Global Options:

Options:
--cwd working directory to use (defaults to cwd)
--destination, --dest, -d Directory to save the packed archive to. [string]
--destination, --dest, -d Directory to save the packed archive to.
[string] [default: ./build/bundle]
--filename Override the filename of the archive.
[string] [default: {app-name}-{version}.zip]
--app-name The name of the app to replace in filename
[string] [default: ${config.name}]
--app-version The version of the app to replace in filename
[string] [default: ${config.version}]
```

0 comments on commit 9891ea3

Please sign in to comment.