@embeddable.com/sdk-core 0.0.1

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.
package/bin/embeddable ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const { push } = require("../src/push");
5
+ const { build } = require("../src/build");
6
+
7
+ async function main () {
8
+ const command = process.argv[2];
9
+
10
+ const runScript = command === 'build' ? build : push;
11
+
12
+ await runScript();
13
+ }
14
+
15
+ main()
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@embeddable.com/sdk-core",
3
+ "version": "0.0.1",
4
+ "description": "TODO.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/embeddable-hq/embeddable-sdk.git",
8
+ "directory": "packages/core"
9
+ },
10
+ "author": "Oleg Kapustin <oleg@trevor.io>",
11
+ "files": [
12
+ "src/"
13
+ ],
14
+ "bin": {
15
+ "embeddable": "bin/embeddable"
16
+ },
17
+ "license": "MIT",
18
+ "dependencies": {
19
+ "@stencil/core": "^4.0.0",
20
+ "js-yaml": "^4.1.0",
21
+ "prompt": "^1.3.0"
22
+ }
23
+ }
package/src/build.js ADDED
@@ -0,0 +1,20 @@
1
+ const path = require("path");
2
+ const { prepare } = require("./prepare");
3
+ const { generate } = require("./generate");
4
+ const { cleanup } = require("./cleanup");
5
+ const {createContext} = require("./createContext");
6
+
7
+ async function build(pluginOptions = {}) {
8
+ const ctx = {
9
+ ...createContext(path.resolve(__dirname, '..'), process.cwd()),
10
+ pluginOptions
11
+ };
12
+
13
+ await prepare(ctx);
14
+
15
+ await generate(ctx);
16
+
17
+ await cleanup(ctx);
18
+ }
19
+
20
+ module.exports = { build };
package/src/cleanup.js ADDED
@@ -0,0 +1,30 @@
1
+ const fs = require('fs/promises');
2
+ const path = require("path");
3
+
4
+ async function cleanup (ctx) {
5
+ await extractBuild(ctx);
6
+
7
+ await removeObsoleteDir(ctx.client.buildDir);
8
+
9
+ await moveBuildTOBuildDir(ctx);
10
+ }
11
+
12
+ module.exports = { cleanup };
13
+
14
+
15
+ async function extractBuild(ctx) {
16
+ await fs.rename(
17
+ path.resolve(ctx.client.buildDir, ctx.client.stencilBuild),
18
+ ctx.client.tmpDir
19
+ );
20
+ }
21
+
22
+
23
+ async function removeObsoleteDir(dir) {
24
+ await fs.rm(dir, { recursive: true });
25
+ }
26
+
27
+
28
+ async function moveBuildTOBuildDir(ctx) {
29
+ await fs.rename(ctx.client.tmpDir, ctx.client.buildDir);
30
+ }
@@ -0,0 +1,20 @@
1
+ const path = require('path');
2
+
3
+ function createContext (coreRoot, clientRoot) {
4
+ return {
5
+ core: {
6
+ rootDir: coreRoot,
7
+ templatesDir: path.resolve(coreRoot, 'templates'),
8
+ configsDir: path.resolve(coreRoot, 'configs')
9
+ },
10
+ client: {
11
+ rootDir: clientRoot,
12
+ buildDir: path.resolve(clientRoot, '.embeddable-build'),
13
+ tmpDir: path.resolve(clientRoot, '.embeddable-tmp'),
14
+ componentDir: path.resolve(clientRoot, '.embeddable-build', 'component'),
15
+ stencilBuild: path.resolve(clientRoot, '.embeddable-build', 'dist', 'embeddable-wrapper')
16
+ }
17
+ }
18
+ }
19
+
20
+ module.exports = { createContext };
@@ -0,0 +1,68 @@
1
+ const fs = require("fs/promises");
2
+ const path = require("path");
3
+
4
+ const stencilNodeApi = require("@stencil/core/sys/node");
5
+ const stencil = require("@stencil/core/cli");
6
+
7
+
8
+ const STYLE_IMPORTS_TOKEN = '{{STYLES_IMPORT}}';
9
+ const RENDER_IMPORT_TOKEN = '{{RENDER_IMPORT}}';
10
+
11
+ const NODE_LOGGER = stencilNodeApi.createNodeLogger({ process: process });
12
+ const NODE_SYS = stencilNodeApi.createNodeSys({ process: process, logger: NODE_LOGGER });
13
+
14
+ async function generate(ctx) {
15
+ await injectCSS(ctx);
16
+
17
+ await injectBundleRender(ctx);
18
+
19
+ await runStencil(ctx);
20
+ }
21
+
22
+ module.exports = { generate };
23
+
24
+ async function injectCSS (ctx) {
25
+ const CUSTOMER_BUILD = path.resolve(ctx.client.buildDir, ctx.pluginOptions.outDir);
26
+ const allFiles = await fs.readdir(CUSTOMER_BUILD);
27
+
28
+ const cssFilesImportsStr = allFiles
29
+ .filter(fileName => fileName.endsWith('.css'))
30
+ .map(fileName => `@import '../${ctx.pluginOptions.outDir}/${fileName}';`).join('\n');
31
+
32
+ const content = await fs.readFile(
33
+ path.resolve(ctx.core.templatesDir, 'style.css.template'),
34
+ 'utf8'
35
+ );
36
+
37
+ await fs.writeFile(
38
+ path.resolve(ctx.client.componentDir, 'style.css'),
39
+ content.replace(STYLE_IMPORTS_TOKEN, cssFilesImportsStr)
40
+ );
41
+ }
42
+
43
+
44
+ async function injectBundleRender(ctx) {
45
+ const importStr = `import render from '../${ctx.pluginOptions.outDir}/${ctx.pluginOptions.renderFunctionFileName}.js';`;
46
+
47
+ const content = await fs.readFile(
48
+ path.resolve(ctx.core.templatesDir, 'component.tsx.template'),
49
+ 'utf8'
50
+ );
51
+
52
+ await fs.writeFile(
53
+ path.resolve(ctx.client.componentDir, 'component.tsx'),
54
+ content.replace(RENDER_IMPORT_TOKEN, importStr)
55
+ );
56
+ }
57
+
58
+
59
+ async function runStencil(ctx) {
60
+ process.chdir(ctx.client.buildDir);
61
+
62
+ await stencil.run({
63
+ args: ['build'],
64
+ logger: NODE_LOGGER,
65
+ sys: NODE_SYS,
66
+ checkVersion: stencilNodeApi.checkVersion,
67
+ });
68
+ }
package/src/prepare.js ADDED
@@ -0,0 +1,26 @@
1
+ const fsSync = require('fs');
2
+ const fs = require('fs/promises');
3
+
4
+ async function prepare (ctx) {
5
+ await removeIfExists(ctx);
6
+
7
+ await copyStencilConfigsToClient(ctx);
8
+
9
+ await createComponentDir(ctx.client.componentDir);
10
+ }
11
+
12
+ module.exports = { prepare }
13
+
14
+ async function removeIfExists(ctx) {
15
+ if (ctx.pluginOptions) return;
16
+
17
+ if (fsSync.existsSync(ctx.client.buildDir)) await fs.rm(ctx.client.buildDir, { recursive: true });
18
+ }
19
+
20
+ async function copyStencilConfigsToClient(ctx) {
21
+ await fs.cp(ctx.core.configsDir, ctx.client.buildDir, { recursive: true });
22
+ }
23
+
24
+ async function createComponentDir(dir) {
25
+ await fs.mkdir(dir);
26
+ }
package/src/push.js ADDED
@@ -0,0 +1,40 @@
1
+ const fsSync = require("fs");
2
+ const { execSync } = require("child_process");
3
+ const prompt = require('prompt');
4
+ const {createContext} = require("./createContext");
5
+ const path = require("path");
6
+
7
+ const PROMPT_SCHEMA = {
8
+ properties: {
9
+ clientName: {
10
+ description: 'Client name',
11
+ type: 'string',
12
+ default: 'customer-1'
13
+ }
14
+ }
15
+ };
16
+
17
+ /**
18
+ * TODO:
19
+ * 1. use client id from the login session instead of prompting clientName.
20
+ * 2. use our API endpoint to send archived bundle files + meta yaml files.
21
+ */
22
+ async function push () {
23
+ const ctx = createContext(path.resolve(__dirname, '..'), process.cwd());
24
+
25
+ prompt.start();
26
+
27
+ const { clientName } = await prompt.get(PROMPT_SCHEMA);
28
+
29
+
30
+ if (!fsSync.existsSync(ctx.client.buildDir)) {
31
+ console.error('No embeddable build was produced.');
32
+ process.exit(1);
33
+ }
34
+
35
+ execSync(`gsutil -m rm -r gs://embeddable-bundle-tmp/${clientName}`, {cwd: ctx.client.rootDir});
36
+ execSync(`gsutil -m cp -r .embeddable-build gs://embeddable-bundle-tmp/${clientName}`, {cwd: ctx.client.rootDir});
37
+ execSync(`gsutil -m setmeta -h "Cache-Control: no-store" gs://embeddable-bundle-tmp/${clientName}/**`);
38
+ }
39
+
40
+ module.exports = { push }