1 | var dedent = require('dedent')
|
2 | var rimraf = require('rimraf')
|
3 | var path = require('path')
|
4 | var tape = require('tape')
|
5 | var fs = require('fs')
|
6 | var os = require('os')
|
7 |
|
8 | var bankai = require('../')
|
9 | var tmpDirname
|
10 |
|
11 | function cleanup () {
|
12 | rimraf.sync(tmpDirname)
|
13 | }
|
14 |
|
15 | tape('read a manifest', function (assert) {
|
16 | assert.on('end', cleanup)
|
17 | assert.plan(5)
|
18 | var script = dedent`
|
19 | 1 + 1
|
20 | `
|
21 |
|
22 | var manifest = dedent`
|
23 | {
|
24 | "name": "demo",
|
25 | "short_name": "demo",
|
26 | "description": "A very cute app",
|
27 | "start_url": "/",
|
28 | "display": "standalone",
|
29 | "background_color": "#ffc0cb",
|
30 | "theme_color": "#ffc0cb",
|
31 | "icons": [{
|
32 | "src": "/assets/icon.png",
|
33 | "type": "image/png",
|
34 | "sizes": "512x512"
|
35 | }]
|
36 | }
|
37 | `
|
38 |
|
39 | var dirname = 'manifest-pipeline-' + (Math.random() * 1e4).toFixed()
|
40 | tmpDirname = path.join(os.tmpdir(), dirname)
|
41 | var tmpScriptname = path.join(tmpDirname, 'index.js')
|
42 | var tmpManifestname = path.join(tmpDirname, 'manifest.json')
|
43 |
|
44 | fs.mkdirSync(tmpDirname)
|
45 | fs.writeFileSync(tmpScriptname, script)
|
46 | fs.writeFileSync(tmpManifestname, manifest)
|
47 |
|
48 | var compiler = bankai(tmpScriptname, { watch: false })
|
49 | compiler.manifest(function (err, res) {
|
50 | assert.error(err, 'no error writing manifest')
|
51 | assert.ok(res, 'output exists')
|
52 | assert.ok(res.buffer, 'output buffer exists')
|
53 | assert.ok(res.hash, 'output hash exists')
|
54 | })
|
55 |
|
56 | compiler.scripts('bundle.js', function (err, res) {
|
57 | assert.error(err, 'no error writing script')
|
58 | })
|
59 | })
|
60 |
|
61 | tape('should provide a default manifest', function (assert) {
|
62 | assert.on('end', cleanup)
|
63 | assert.plan(3)
|
64 |
|
65 | var script = dedent`
|
66 | 1 + 1
|
67 | `
|
68 |
|
69 | var dirname = 'manifest-pipeline-' + (Math.random() * 1e4).toFixed()
|
70 | tmpDirname = path.join(os.tmpdir(), dirname)
|
71 | var tmpScriptname = path.join(tmpDirname, 'index.js')
|
72 |
|
73 | fs.mkdirSync(tmpDirname)
|
74 | fs.writeFileSync(tmpScriptname, script)
|
75 |
|
76 | var compiler = bankai(tmpScriptname, { watch: false })
|
77 | compiler.manifest(function (err, res) {
|
78 | assert.error(err, 'no error writing manifest')
|
79 | assert.ok(res, 'output exists')
|
80 | })
|
81 |
|
82 | compiler.scripts('bundle.js', function (err, res) {
|
83 | assert.error(err, 'no error writing script')
|
84 | })
|
85 | })
|
86 |
|
87 | tape('should watch the manifest for changes', function (assert) {
|
88 | assert.on('end', function () {
|
89 | compiler.close()
|
90 | cleanup()
|
91 | })
|
92 | assert.plan(12)
|
93 |
|
94 | var script = dedent`
|
95 | 1 + 1
|
96 | `
|
97 |
|
98 | var manifest1 = dedent`
|
99 | { "name": "foo" }
|
100 | `
|
101 |
|
102 | var manifest2 = dedent`
|
103 | { "name": "bar" }
|
104 | `
|
105 |
|
106 | var dirname = 'manifest-pipeline-' + (Math.random() * 1e4).toFixed()
|
107 | tmpDirname = path.join(os.tmpdir(), dirname)
|
108 | var tmpScriptname = path.join(tmpDirname, 'index.js')
|
109 | var tmpManifestname = path.join(tmpDirname, 'manifest.json')
|
110 |
|
111 | fs.mkdirSync(tmpDirname)
|
112 | fs.writeFileSync(tmpScriptname, script)
|
113 | fs.writeFileSync(tmpManifestname, manifest1)
|
114 |
|
115 | var compiler = bankai(tmpScriptname)
|
116 | compiler.manifest(function (err, res) {
|
117 | assert.error(err, 'no error writing manifest')
|
118 | assert.ok(res, 'output exists')
|
119 | assert.ok(res.buffer, 'output buffer exists')
|
120 | assert.ok(res.hash, 'output hash exists')
|
121 | assert.ok(/foo/.exec(String(res.buffer)), 'contains foo')
|
122 |
|
123 | compiler.on('change', function (nodeName) {
|
124 | if (nodeName !== 'manifest') return
|
125 | compiler.manifest(function (err, res) {
|
126 | assert.error(err, 'no error writing manifest')
|
127 | assert.ok(res, 'output exists')
|
128 | assert.ok(res.buffer, 'output buffer exists')
|
129 | assert.ok(res.hash, 'output hash exists')
|
130 | assert.ok(/bar/.exec(String(res.buffer)), 'contains bar')
|
131 | })
|
132 | })
|
133 |
|
134 | fs.writeFile(tmpManifestname, manifest2, function (err) {
|
135 | assert.error(err, 'no error writing manifest 2')
|
136 | })
|
137 | })
|
138 |
|
139 | compiler.scripts('bundle.js', function (err, res) {
|
140 | assert.error(err, 'no error writing script')
|
141 | })
|
142 | })
|