diff --git a/build/builder.py b/build/builder.py index 4a8dd7a1..a861bff8 100644 --- a/build/builder.py +++ b/build/builder.py @@ -20,32 +20,24 @@ def build(ctx, config, version): \b Runs the following in order: - wq init (if using wq optimize) + wq init (if using r.js) wq setversion wq collectjson (if configured) wq scss (if configured) wq mustache (if configured) - - \b - Followed by either: - wq optimize + wq babel + wq appcache - Or: - npm build + npm build (if using npm) + wq optimize (if using r.js) + wq babel (if configured & using r.js) + wq appcache (if configured & using r.js) """ has_package_json = os.path.exists( os.path.join(os.path.dirname(config.filename), 'package.json') ) - if has_package_json: - if 'optimize' in config: - raise click.UsageError( - "optimize section is not used for npm build" - ) - else: - if 'optimize' not in config: - raise click.UsageError( - "optimize section not found in %s" % config.filename - ) + if has_package_json and 'optimize' in config: + raise click.UsageError( + "optimize section is not used for npm build" + ) def run(command, **kwargs): confs = config.get(command.name, {}) @@ -55,7 +47,7 @@ def run(command, **kwargs): conf.update(kwargs) ctx.invoke(command, **conf) - if not has_package_json: + if 'optimize' in config: run(init) # Save version information @@ -70,7 +62,7 @@ def run(command, **kwargs): ['npm', 'build'], cwd=os.path.abspath(os.path.dirname(config.filename)) ) - else: + elif 'optimize' in config: # Compile Javascript / CSS (using r.js) ctx.invoke(optimize) diff --git a/build/setversion.py b/build/setversion.py index e65304e5..3b24a457 100644 --- a/build/setversion.py +++ b/build/setversion.py @@ -1,6 +1,7 @@ import os from wq.core import wq import click +import re @wq.command() @@ -9,6 +10,8 @@ help="Name of text file (default is version.txt)" ) @click.option('--jsout', help="Name of an AMD module (e.g. myapp/version.js)") +@click.option('--esm', help="Name of an ESM module (e.g. myapp/version.js)") +@click.option('--package', help="Path to package.json") @click.argument('version') def setversion(**conf): """ @@ -27,16 +30,29 @@ def setversion(**conf): vtxt.write(version) vtxt.close() - if conf['jsout']: + if conf['esm'] or conf['jsout']: # Update version.js - vjs = open(conf['jsout'], 'w') - vjs.write(VERSIONJS_TMPL % version) - vjs.close() - click.echo('%s: %s' % (conf['jsout'], version)) + if conf['esm']: + js_file = conf['esm'] + js_tmpl = """export default "%s";""" + else: + js_file = conf['jsout'] + js_tmpl = """define(function(){return "%s";});""" + with open(js_file, 'w') as f: + f.write(js_tmpl % version) + click.echo('%s: %s' % (js_file, version)) else: click.echo('Application version: %s' % version) - return version - + if conf['package']: + with open(conf['package']) as f: + content = f.read() + content = re.sub( + '"version": "[^"]+"', + '"version": "%s"' % version, + content + ) + with open(conf['package'], 'w') as f: + f.write(content) -VERSIONJS_TMPL = """define(function(){return "%s";});""" + return version