@applicaster/zapplicaster-cli 16.0.0-rc.13 → 16.0.0-rc.130

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/package.json +5 -5
  2. package/src/commands/prepareWorkspace/__tests__/__snapshots__/configurator.test.js.snap +2 -0
  3. package/src/commands/prepareWorkspace/__tests__/buildPluginsCreator.test.js +99 -0
  4. package/src/commands/prepareWorkspace/appBootstrapper.js +9 -0
  5. package/src/commands/prepareWorkspace/buildPluginsCreator.js +61 -0
  6. package/src/commands/prepareWorkspace/configFilesRetriever.js +3 -0
  7. package/src/commands/prepareWorkspace/configurator.js +26 -1
  8. package/src/commands/prepareWorkspace/parseFonts.js +6 -0
  9. package/src/commands/publishPlugin/README.md +140 -0
  10. package/src/commands/publishPlugin/__tests__/commitToGit.test.js +1 -1
  11. package/src/commands/publishPlugin/__tests__/configurator.test.js +2 -2
  12. package/src/commands/publishPlugin/__tests__/manifests/android.json +3 -1
  13. package/src/commands/publishPlugin/__tests__/manifests/android_for_quickbrick.json +6 -0
  14. package/src/commands/publishPlugin/__tests__/manifests/android_tv_for_quickbrick.json +6 -0
  15. package/src/commands/publishPlugin/__tests__/manifests/ios.json +3 -1
  16. package/src/commands/publishPlugin/__tests__/manifests/ios_for_quickbrick.json +6 -0
  17. package/src/commands/publishPlugin/__tests__/manifests/tvos_for_quickbrick.json +6 -0
  18. package/src/commands/publishPlugin/__tests__/npmPublish.test.js +39 -23
  19. package/src/commands/publishPlugin/__tests__/zappApi.test.js +271 -0
  20. package/src/commands/publishPlugin/__tests__/zappifestPublish.test.js +109 -33
  21. package/src/commands/publishPlugin/commitToGit.js +1 -1
  22. package/src/commands/publishPlugin/configurator.js +1 -1
  23. package/src/commands/publishPlugin/generateManifest.js +1 -0
  24. package/src/commands/publishPlugin/npmPublish.js +6 -9
  25. package/src/commands/publishPlugin/updateCorePluginsIfNeeded.js +29 -12
  26. package/src/commands/publishPlugin/zappApi.js +283 -0
  27. package/src/commands/publishPlugin/zappifestPublish.js +123 -8
  28. package/src/commands/reloadConfig/__tests__/__snapshots__/reloadConfig.test.js.snap +58 -0
  29. package/src/commands/reloadConfig/__tests__/reloadConfig.test.js +86 -0
  30. package/src/commands/reloadConfig/index.js +6 -6
  31. package/src/download/__tests__/download.test.js +53 -0
  32. package/src/download/index.js +65 -1
  33. package/src/file/__tests__/file.test.js +7 -1
  34. package/src/file/index.js +29 -12
  35. package/src/templates/__tests__/__snapshots__/templates.test.js.snap +1 -1
  36. package/src/templates/index.js +1 -0
  37. package/test_helpers/fsMock.js +11 -0
  38. package/src/commands/publishPlugin/__tests__/__snapshots__/zappifestPublish.test.js.snap +0 -5
@@ -1,4 +1,5 @@
1
1
  const R = require("ramda");
2
+ const fs = require("fs");
2
3
  const axios = require("axios");
3
4
  const { resolve } = require("path");
4
5
  const { curlFile, curlMultipleFiles, unzip } = require("../shell");
@@ -12,6 +13,64 @@ const getFileName = R.ifElse(
12
13
  R.prop("name")
13
14
  );
14
15
 
16
+ // Local file signature for a zip archive: "PK\x03\x04"
17
+ const ZIP_SIGNATURE = "504b0304";
18
+
19
+ /**
20
+ * checks whether a file on disk is a real zip archive by reading its magic bytes.
21
+ * @param {String} file path to the file to inspect
22
+ * @returns {Boolean} true if the file starts with the zip local file signature
23
+ */
24
+ function isZipFile(file) {
25
+ const fd = fs.openSync(file, "r");
26
+
27
+ try {
28
+ const header = Buffer.alloc(4);
29
+ const bytesRead = fs.readSync(fd, header, 0, 4, 0);
30
+
31
+ return bytesRead === 4 && header.toString("hex") === ZIP_SIGNATURE;
32
+ } finally {
33
+ fs.closeSync(fd);
34
+ }
35
+ }
36
+
37
+ /**
38
+ * verifies that the downloaded assets bundle is a real zip archive.
39
+ * The assets CDN responds with a small XML/HTML body (e.g. S3 "AccessDenied")
40
+ * when the bundle is missing or not yet published. Because the download uses
41
+ * `curl -O` without `-f`, that error body is saved as assets.zip and the process
42
+ * continues, only to crash later in webpack with a confusing parse error. This
43
+ * check fails fast in the prepare step with an actionable message instead.
44
+ * @param {String} file path to the downloaded assets bundle
45
+ * @param {String} assetsUrl original url, surfaced in the error message
46
+ */
47
+ function assertValidAssetsBundle(file, assetsUrl) {
48
+ if (!fs.existsSync(file)) {
49
+ throw new Error(
50
+ `Assets bundle was not downloaded from ${assetsUrl}. ` +
51
+ "Verify the app has a published build with generated assets for this platform."
52
+ );
53
+ }
54
+
55
+ if (isZipFile(file)) {
56
+ return;
57
+ }
58
+
59
+ const preview = fs
60
+ .readFileSync(file, "utf8")
61
+ .replace(/\s+/g, " ")
62
+ .trim()
63
+ .slice(0, 300);
64
+
65
+ throw new Error(
66
+ `Invalid assets bundle downloaded from ${assetsUrl} - expected a zip archive ` +
67
+ `but received non-zip content:\n "${preview}"\n` +
68
+ "This usually means the assets bundle has not been generated/published for " +
69
+ 'this platform, or the URL is not accessible (e.g. S3 "AccessDenied"). ' +
70
+ "Publish a build with assets for this platform before preparing the workspace."
71
+ );
72
+ }
73
+
15
74
  /**
16
75
  * saves a remote json file in the app's configuration folder.
17
76
  * curried function of the shape saveRemoteJsonToFile(configuration)(fileUrl)
@@ -65,7 +124,12 @@ function saveAssets({ destinationPath, platform }, assetsUrl) {
65
124
  });
66
125
 
67
126
  curlFile(assetsUrl, filePath);
68
- unzip(resolve(filePath, fileName), filePath);
127
+
128
+ const downloadedFile = resolve(filePath, fileName);
129
+
130
+ assertValidAssetsBundle(downloadedFile, assetsUrl);
131
+
132
+ unzip(downloadedFile, filePath);
69
133
  }
70
134
 
71
135
  /**
@@ -22,10 +22,16 @@ describe("writeJsonTofile", () => {
22
22
  await writeJsonToFile(filePath, json);
23
23
 
24
24
  expect(fs.writeFile).toHaveBeenCalledWith(
25
- filePath,
25
+ expect.stringMatching(/^someFile\.json\.tmp\./),
26
26
  JSON.stringify(json, null, 2),
27
27
  expect.any(Function)
28
28
  );
29
+
30
+ expect(fs.rename).toHaveBeenCalledWith(
31
+ expect.stringMatching(/^someFile\.json\.tmp\./),
32
+ filePath,
33
+ expect.any(Function)
34
+ );
29
35
  });
30
36
  });
31
37
 
package/src/file/index.js CHANGED
@@ -5,23 +5,34 @@ const R = require("ramda");
5
5
  * @module @applicaster/zapplicaster-cli/util/file
6
6
  */
7
7
 
8
- const { writeFile, readFile, readdir, copyFile, copyFileSync } = require("fs");
8
+ const {
9
+ writeFile,
10
+ readFile,
11
+ readdir,
12
+ copyFile,
13
+ copyFileSync,
14
+ rename,
15
+ } = require("fs");
16
+
9
17
  const { promisify } = require("util");
10
18
  const { join } = require("path");
11
19
 
12
20
  const writeFileAsync = promisify(writeFile);
21
+ const renameAsync = promisify(rename);
13
22
  const readFileAsync = promisify(readFile);
14
23
  const readdirAsync = promisify(readdir);
15
24
  const copyFileAsync = promisify(copyFile);
16
25
 
17
26
  /**
18
- * Writes a pretty-printed json file from a javascript object
27
+ * Writes a pretty-printed json file from a javascript object atomically
19
28
  * @param {String} path : path of the file to write
20
29
  * @param {Object} object : object to write to the file
21
30
  * @returns {Promise}
22
31
  */
23
32
  async function writeJsonToFile(path, object) {
24
- return await writeFileAsync(path, JSON.stringify(object, null, 2));
33
+ const tmpPath = `${path}.tmp.${Date.now()}.${Math.random().toString(36).slice(2, 6)}`;
34
+ await writeFileAsync(tmpPath, JSON.stringify(object, null, 2));
35
+ await renameAsync(tmpPath, path);
25
36
  }
26
37
 
27
38
  /**
@@ -61,8 +72,13 @@ function destinationFileNameMapper(destinationFileName) {
61
72
  /**
62
73
  * Copies an array of files from a folder to another
63
74
  *
75
+ * Each entry can either be a string (same relative path for source and
76
+ * destination) or an object `{ source, destination }` when a file should be
77
+ * read from one relative path and written to another. The latter lets several
78
+ * platforms reuse a single set of bundled assets without duplicating them.
79
+ *
64
80
  * @param {String} source folder of the files
65
- * @param {String[]} files list of the files
81
+ * @param {(String|{source: String, destination: String})[]} files list of the files
66
82
  * @param {String} destination path
67
83
  * @returns {Promise[]} array of promises with the status of the copied files
68
84
  */
@@ -72,14 +88,15 @@ function copyFiles(source, filesOrFilesGetter, destination, config) {
72
88
  ? filesOrFilesGetter(config)
73
89
  : filesOrFilesGetter;
74
90
 
75
- return R.map(
76
- (file) =>
77
- copyFileSync(
78
- join(source, file),
79
- destinationFileNameMapper(join(destination, file))
80
- ),
81
- files
82
- );
91
+ return R.map((file) => {
92
+ const sourceFile = typeof file === "string" ? file : file.source;
93
+ const destinationFile = typeof file === "string" ? file : file.destination;
94
+
95
+ return copyFileSync(
96
+ join(source, sourceFile),
97
+ destinationFileNameMapper(join(destination, destinationFile))
98
+ );
99
+ }, files);
83
100
  }
84
101
 
85
102
  module.exports = {
@@ -32,4 +32,4 @@ exports[`resolveAppTemplate retrieves the template from the provided path if pro
32
32
  }
33
33
  `;
34
34
 
35
- exports[`resolveAppTemplate throws if resolving a template for an unknown platform 1`] = `"could not find template for foo - available templates: ios,android,samsung_tv,lg_tv,vizio,apple_tv,android_tv,amazon"`;
35
+ exports[`resolveAppTemplate throws if resolving a template for an unknown platform 1`] = `"could not find template for foo - available templates: ios,android,samsung_tv,lg_tv,vidaa,vizio,apple_tv,android_tv,amazon"`;
@@ -27,6 +27,7 @@ const templatesMap = {
27
27
  android: "@applicaster/zapp-react-native-cli-template",
28
28
  samsung_tv: "@applicaster/zapp-react-dom-cli-template",
29
29
  lg_tv: "@applicaster/zapp-react-dom-cli-template",
30
+ vidaa: "@applicaster/zapp-react-dom-cli-template",
30
31
  vizio: "@applicaster/zapp-react-dom-cli-template",
31
32
  apple_tv: "@applicaster/zapp-react-native-tvos-cli-template",
32
33
  android_tv: "@applicaster/zapp-react-native-android-tv-cli-template",
@@ -48,6 +48,16 @@ function copyFileSync() {
48
48
  return { status: "ok" };
49
49
  }
50
50
 
51
+ function rename(oldPath, newPath, callback) {
52
+ const file = fs.__writenFiles.find((f) => f.fileName === oldPath);
53
+
54
+ if (file) {
55
+ file.fileName = newPath;
56
+ }
57
+
58
+ callback(null);
59
+ }
60
+
51
61
  function existsSync(path) {
52
62
  return R.includes(path, R.map(R.prop("fileName"), fs.__writenFiles));
53
63
  }
@@ -87,6 +97,7 @@ fs.readFile = jest.fn(readFile);
87
97
  fs.readFileSync = jest.fn(readFileSync);
88
98
  fs.copyFile = jest.fn(copyFile);
89
99
  fs.copyFileSync = jest.fn(copyFileSync);
100
+ fs.rename = jest.fn(rename);
90
101
  fs.existsSync = jest.fn(existsSync);
91
102
  fs.statSync = jest.fn(statSync);
92
103
 
@@ -1,5 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`publish zappifests it throws if a manifest cannot be found 1`] = `[Error: cannot resolve manifest]`;
4
-
5
- exports[`publish zappifests it throws if the zappifest command fails 1`] = `[Error: zappifest failed]`;