shoplazza-cli 0.0.10 → 0.1.8

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 (61) hide show
  1. package/README.md +2 -6
  2. package/bin/shoplazza +6 -0
  3. package/examples/checkout-extension/README.md +19 -0
  4. package/examples/checkout-extension/extension.config.js +4 -0
  5. package/examples/checkout-extension/extensions/add-shipping-desc/extension.json +10 -0
  6. package/examples/checkout-extension/extensions/add-shipping-desc/src/index.js +7 -0
  7. package/examples/checkout-extension/extensions/ext-1/extension.json +10 -0
  8. package/examples/checkout-extension/extensions/ext-1/src/content.html +3 -0
  9. package/examples/checkout-extension/extensions/ext-1/src/index.html +5 -0
  10. package/examples/checkout-extension/extensions/ext-1/src/index.js +11 -0
  11. package/examples/checkout-extension/extensions/ext-1/src/script.html +3 -0
  12. package/examples/checkout-extension/extensions/ext-1/src/style.html +3 -0
  13. package/examples/checkout-extension/extensions/rewrite-navigate/extension.json +10 -0
  14. package/examples/checkout-extension/extensions/rewrite-navigate/src/content.html +38 -0
  15. package/examples/checkout-extension/extensions/rewrite-navigate/src/index.html +5 -0
  16. package/examples/checkout-extension/extensions/rewrite-navigate/src/index.js +12 -0
  17. package/examples/checkout-extension/extensions/rewrite-navigate/src/script.html +26 -0
  18. package/examples/checkout-extension/extensions/rewrite-navigate/src/style.html +23 -0
  19. package/examples/checkout-extension/package.json +17 -0
  20. package/lib/app/commands/deploy.js +0 -1
  21. package/lib/app/constants.js +22 -5
  22. package/lib/app/login.js +0 -1
  23. package/lib/auth/index.js +42 -0
  24. package/lib/checkout/api.js +156 -0
  25. package/lib/checkout/build/plugin/vite-plugin-add-extension-id.js +25 -0
  26. package/lib/checkout/build/plugin/vite-plugin-transform-extension-html.js +207 -0
  27. package/lib/checkout/build/vite.config.js +34 -0
  28. package/lib/checkout/build.js +39 -0
  29. package/lib/checkout/config.js +97 -0
  30. package/lib/checkout/console.js +32 -0
  31. package/lib/checkout/create.js +132 -0
  32. package/lib/checkout/delete.js +26 -0
  33. package/lib/checkout/deploy.js +59 -0
  34. package/lib/checkout/dev/client.js +73 -0
  35. package/lib/checkout/dev/index.js +142 -0
  36. package/lib/checkout/fields.js +29 -0
  37. package/lib/checkout/index.js +63 -0
  38. package/lib/checkout/preview.js +52 -0
  39. package/lib/checkout/pull.js +10 -0
  40. package/lib/checkout/push.js +140 -0
  41. package/lib/checkout/template/README.md +34 -0
  42. package/lib/checkout/template/_gitignore +4 -0
  43. package/lib/checkout/template/extension.config.js +4 -0
  44. package/lib/checkout/template/extensions/extension-template/extension.json +10 -0
  45. package/lib/checkout/template/extensions/extension-template/src/content.html +3 -0
  46. package/lib/checkout/template/extensions/extension-template/src/index.html +5 -0
  47. package/lib/checkout/template/extensions/extension-template/src/index.js +11 -0
  48. package/lib/checkout/template/extensions/extension-template/src/script.html +3 -0
  49. package/lib/checkout/template/extensions/extension-template/src/style.html +3 -0
  50. package/lib/checkout/template/package.json +17 -0
  51. package/lib/checkout/undeploy.js +40 -0
  52. package/lib/checkout/util.js +204 -0
  53. package/lib/checkout/verify.js +16 -0
  54. package/lib/checkout/version.js +7 -0
  55. package/lib/commands/login.js +3 -2
  56. package/lib/commands/theme/init.js +2 -2
  57. package/lib/commands/theme/pull.js +1 -1
  58. package/lib/config.js +4 -0
  59. package/lib/db/user.js +5 -2
  60. package/lib/utils.js +36 -4
  61. package/package.json +29 -3
@@ -0,0 +1,142 @@
1
+ const inquirer = require('inquirer');
2
+ const {
3
+ getExtensionPathById,
4
+ getExtensionInfo,
5
+ copy,
6
+ getDistPath,
7
+ getExtensionsPath,
8
+ getExtensionConfig
9
+ } = require('../util');
10
+ const { cwd } = require('../config');
11
+ const path = require('path');
12
+ const chokidar = require('chokidar');
13
+ const fs = require('fs');
14
+ const { viteBuild } = require('../build');
15
+ const { consoleError, consoleBlue, consoleSuccess } = require('../console');
16
+
17
+ async function buildExtension(id) {
18
+ return viteBuild({ id })
19
+ .catch(consoleError)
20
+ .then(() => {
21
+ const { distName } = getExtensionInfo(id);
22
+ return distName;
23
+ });
24
+ }
25
+
26
+ let webSocket = undefined;
27
+
28
+ function handleWsConnection(ws) {
29
+ webSocket = ws;
30
+ ws.on('error', console.error);
31
+ ws.on('message', function message(data) {
32
+ console.log(`Received message ${data} from user `);
33
+ });
34
+
35
+ const extensionList = [];
36
+ fs.readdirSync(getExtensionsPath()).forEach((ext) => {
37
+ extensionList.push(generateExtensionData(ext));
38
+ });
39
+ ws.send(JSON.stringify({ event: 'init', data: extensionList }));
40
+
41
+ }
42
+
43
+ async function createWsServer(httpServer) {
44
+ const { WebSocketServer } = require('ws');
45
+ const wss = new WebSocketServer({ noServer: true });
46
+ wss.on('connection', handleWsConnection);
47
+
48
+ httpServer.on('upgrade', function upgrade(request, socket, head) {
49
+ wss.handleUpgrade(request, socket, head, function done(ws) {
50
+ wss.emit('connection', ws, request);
51
+ });
52
+ });
53
+ return wss;
54
+ }
55
+
56
+ function createServer(dir) {
57
+ const connect = require('connect');
58
+ const http = require('http');
59
+ const app = connect();
60
+
61
+ const serveStatic = require('serve-static');
62
+ app.use(
63
+ serveStatic(dir, {
64
+ setHeaders: function (res) {
65
+ res.setHeader('Access-Control-Allow-Origin', '*');
66
+ res.setHeader('Access-Control-Allow-Methods', 'GET');
67
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
68
+ }
69
+ })
70
+ );
71
+ const httpServer = http.createServer(app);
72
+
73
+ return httpServer;
74
+ }
75
+
76
+ function generateExtensionData(id) {
77
+ const info = getExtensionInfo(id);
78
+ const config = getExtensionConfig(id);
79
+ const data = {
80
+ extensionId: info.id,
81
+ resourceUrl: `http://localhost:8888/${info.distName}`,
82
+ status: 'draft',
83
+ version: config.version,
84
+ fields: JSON.stringify(config)
85
+ };
86
+ return data;
87
+ }
88
+
89
+ async function startDev() {
90
+ if (fs.existsSync(getDistPath())) {
91
+ fs.rmdirSync(getDistPath(), { recursive: true });
92
+ }
93
+
94
+ const extensionPath = path.resolve(cwd, './extensions');
95
+ const files = fs.readdirSync(extensionPath);
96
+ const choices = files.filter((file) => {
97
+ return fs.statSync(path.resolve(extensionPath, file)).isDirectory();
98
+ });
99
+ const { extensionNameList } = await inquirer.prompt([
100
+ {
101
+ type: 'checkbox',
102
+ name: 'extensionNameList',
103
+ message: 'Please select one or more extensions to develop:',
104
+ prefix: '*',
105
+ loop: false,
106
+ choices,
107
+ validate(answer) {
108
+ if (answer.length === 0) {
109
+ return 'You must choose at least one extension.';
110
+ }
111
+ return true;
112
+ },
113
+ }
114
+ ]);
115
+
116
+ // build
117
+ const willBuildExts = extensionNameList;
118
+ await Promise.all(willBuildExts.map((id) => buildExtension(id)));
119
+
120
+ // 复制index.js文件
121
+ copy(path.join(__dirname, 'client.js'), path.join(getDistPath(), 'index.js'));
122
+
123
+ // 监听文件改变
124
+ chokidar.watch(getExtensionsPath()).on('change', async (event) => {
125
+ const id = event.split('extensions/').pop().split('/').shift();
126
+ if (id) {
127
+ consoleBlue(`update extension: ${id}`);
128
+ await buildExtension(id);
129
+ webSocket && webSocket.send(JSON.stringify({ event: 'update', data: generateExtensionData(id) }));
130
+ }
131
+ });
132
+
133
+ // 启动服务
134
+ const httpServer = createServer(getDistPath());
135
+ createWsServer(httpServer, willBuildExts);
136
+ httpServer.listen(8888);
137
+ consoleSuccess('started server on url: http://localhost:8888/index.js');
138
+ }
139
+
140
+ module.exports = {
141
+ startDev
142
+ };
@@ -0,0 +1,29 @@
1
+ const api = require('./api');
2
+ const { consoleError } = require('./console');
3
+ const { verifyConfig } = require('./verify');
4
+ const { getExtensionInfo, getExtensionConfig } = require('./util');
5
+
6
+ async function updateConfig(commands = {}) {
7
+ await verifyConfig(commands);
8
+ const info = getExtensionInfo(commands.id);
9
+ const config = getExtensionConfig(commands.id);
10
+ const data = {
11
+ extension_id: info.id,
12
+ fields: JSON.stringify(config),
13
+ version: config.version
14
+ };
15
+ try {
16
+ if (commands.update) {
17
+ await api.updateFields(data);
18
+ } else {
19
+ await api.pushFields(data);
20
+ }
21
+ } catch (err) {
22
+ consoleError('update config fail.');
23
+ throw 'update_fields_error';
24
+ }
25
+ }
26
+
27
+ module.exports = {
28
+ updateConfig
29
+ };
@@ -0,0 +1,63 @@
1
+ function makeCheckoutCommand(_program) {
2
+ const program = _program.command('checkout').description('Shoplazza checkout cli');
3
+
4
+ program.command('create').description('create a extension project').action(require('./create').createProject);
5
+
6
+ program.command('extension').description('create a new extension').action(require('./create').createExtension);
7
+
8
+ program
9
+ .command('build')
10
+ .description('Some commands related to the build')
11
+ .option('-i, --id <token>', 'extension id (option)')
12
+ .option('-d, --debug', 'debug mode')
13
+ .action(require('./build').cliBuild);
14
+
15
+ program
16
+ .command('config')
17
+ .description('update config')
18
+ .option('-i, --id <token>', 'extension id')
19
+ .option('-u --update', 'is update extension?')
20
+ .action(require('./fields').updateConfig);
21
+
22
+ program
23
+ .command('list')
24
+ .description('fetch extension list.')
25
+ .option('-i, --id <token>', 'extension id')
26
+ .action(require('./pull'));
27
+
28
+ program
29
+ .command('delete')
30
+ .description('delete extension')
31
+ .option('-i, --id <token>', 'extension id')
32
+ .action(require('./delete'));
33
+
34
+ program
35
+ .command('dev')
36
+ .description('dev mode')
37
+ .option('-i, --id <token>', 'extension id')
38
+ .action(require('./dev').startDev);
39
+
40
+ program
41
+ .command('preview')
42
+ .description('preview extension')
43
+ .action(require('./preview'));
44
+
45
+ program
46
+ .command('push')
47
+ .description('push extension.')
48
+ .action(require('./push'));
49
+
50
+ program
51
+ .command('deploy')
52
+ .description('deploy extension')
53
+ .action(require('./deploy'));
54
+
55
+ program
56
+ .command('undeploy')
57
+ .description('undeploy extension')
58
+ .action(require('./undeploy'));
59
+ }
60
+
61
+ module.exports = {
62
+ makeCheckoutCommand
63
+ };
@@ -0,0 +1,52 @@
1
+ const inquirer = require('inquirer');
2
+ const api = require('./api');
3
+ const { verifyConfig } = require('./verify');
4
+ const { useSelectExtensionMode, getProjectConfig } = require('./util');
5
+ const { consoleSuccess, consoleError } = require('./console');
6
+
7
+ module.exports = async () => {
8
+ try {
9
+ await verifyConfig();
10
+ const extensionInfo = await initSelectedExtensionInfo();
11
+ await previewExtension(extensionInfo);
12
+ } catch (error) {
13
+ error.response ? consoleError(`Error: ${error.response.status}-${error.response.config.url}`) : consoleError(error);
14
+ }
15
+ };
16
+
17
+ async function initSelectedExtensionInfo() {
18
+ const res = await api.getExtensionList();
19
+ const extensionList = res.data?.data?.extensions || [];
20
+ return useSelectExtensionMode(extensionList, 'preview');
21
+ }
22
+
23
+ async function previewExtension(extensionInfo) {
24
+ const resp = await api.getVersionList({
25
+ extension_id: extensionInfo.extension_id
26
+ });
27
+ const versionList = resp.data?.data?.extensions || [];
28
+ const choices = versionList.map((item) => ({
29
+ name: `v${item.version}${item.publish_status === 'published' ? '(Published)' : ''}`,
30
+ value: item.id
31
+ }));
32
+ const { versionId } = await inquirer.prompt([
33
+ {
34
+ type: 'list',
35
+ name: 'versionId',
36
+ message: `Please select a version`,
37
+ prefix: '*',
38
+ loop: false,
39
+ choices
40
+ }
41
+ ]);
42
+ const versionInfo = versionList.find((item) => item.id === versionId);
43
+ const res = await api.previewExtension({
44
+ extension_id: versionInfo.extension_id,
45
+ id: versionInfo.id
46
+ });
47
+ const checkoutUrl = res.data?.data?.checkout_url;
48
+ const { store: baseUrl } = getProjectConfig();
49
+ const url = new URL(checkoutUrl, baseUrl);
50
+ url.searchParams.append('step', 'contact_information');
51
+ consoleSuccess(`Your extension's preview URL: ${url.toString()}`);
52
+ }
@@ -0,0 +1,10 @@
1
+ const api = require('./api');
2
+ const { verifyConfig } = require('./verify');
3
+
4
+ module.exports = async (commands = {}) => {
5
+ try {
6
+ await verifyConfig(commands);
7
+ const res = await api.getExtension();
8
+ console.log(res.data?.extension_lists);
9
+ } catch (err) {}
10
+ };
@@ -0,0 +1,140 @@
1
+ const fs = require('fs');
2
+ const inquirer = require('inquirer');
3
+ const api = require('./api');
4
+ const loading = require('loading-cli');
5
+ const {
6
+ getExtensionInfo,
7
+ getExtensionConfig,
8
+ getDistPath,
9
+ useSelectExtensionMode,
10
+ setExtensionConfig,
11
+ checkLocalExtension,
12
+ getProjectConfig
13
+ } = require('./util');
14
+ const fsExtra = require('fs-extra');
15
+ const { consoleBlue, consoleSuccess, consoleError, consoleWarn } = require('./console');
16
+ const { viteBuild } = require('./build');
17
+ const { verifyConfig } = require('./verify');
18
+
19
+ module.exports = async () => {
20
+ try {
21
+ await verifyConfig();
22
+ const extensionInfo = await initSelectedExtensionInfo();
23
+ consoleBlue('Starting to build:');
24
+ const composeCommands = {
25
+ id: extensionInfo.name
26
+ }; // 兼容旧代码处理
27
+ await viteBuild(composeCommands);
28
+ consoleBlue('Starting to upload:');
29
+ await uploadOSS(composeCommands);
30
+ consoleBlue('Starting to push:');
31
+ await pushExtension(composeCommands);
32
+ } catch (error) {
33
+ error.response ? consoleError(`Error: ${error.response.status}-${error.response.config.url}`) : consoleError(error);
34
+ }
35
+ };
36
+
37
+ /**
38
+ * 上传至oss
39
+ */
40
+ async function uploadOSS(command) {
41
+ const info = getExtensionInfo(command.id);
42
+ const dir = getDistPath();
43
+ if (!fs.existsSync(dir)) {
44
+ consoleError('The dist directory does not exist.');
45
+ throw 'not_exist_dist';
46
+ }
47
+ const files = fsExtra.readdirSync(dir).filter((name) => name.includes(info.id));
48
+ try {
49
+ await api.uploadDir(dir, files);
50
+ } catch (error) {
51
+ if (error.response?.data.includes('<Code>FileAlreadyExists</Code>')) {
52
+ consoleWarn('The current file already exists, not need to upload.');
53
+ return;
54
+ } else {
55
+ consoleError('Failed to upload to OSS:', error);
56
+ throw 'upload_failed';
57
+ }
58
+ }
59
+ consoleSuccess('Successfully uploaded to OSS.');
60
+ }
61
+
62
+ /**
63
+ * 发布
64
+ * @returns
65
+ */
66
+ async function pushExtension(command) {
67
+ const info = getExtensionInfo(command.id);
68
+ const load = loading(`start push the extension ${info.id}.`).start();
69
+ try {
70
+ const config = getExtensionConfig(command.id);
71
+ const data = {
72
+ resource_url: `https://cn.static.shoplazza.com/chick-extension/${info.distName}`,
73
+ version: config.version,
74
+ scope: '',
75
+ template_name: config.templateName,
76
+ theme_name: config.themeName,
77
+ name: config.extensionName,
78
+ description: config.extensionDescription
79
+ };
80
+ const isFirstPush = !config.extensionId; // 非首次推送
81
+ if (!isFirstPush) {
82
+ data.extension_id = config.extensionId;
83
+ }
84
+ const res = await api.pushExtension(data, isFirstPush);
85
+ const extensionInfo = res.data?.data?.extension || {};
86
+
87
+ // 填充本地extensionId
88
+ if (isFirstPush) {
89
+ setExtensionConfig(command.id, {
90
+ ...config,
91
+ extensionId: extensionInfo.extension_id
92
+ });
93
+ }
94
+
95
+ // 预览
96
+ const resp = await api.previewExtension({
97
+ extension_id: extensionInfo.extension_id,
98
+ id: extensionInfo.id
99
+ });
100
+ const checkoutUrl = resp.data?.data?.checkout_url;
101
+ const { store: baseUrl } = getProjectConfig();
102
+ const url = new URL(checkoutUrl, baseUrl);
103
+ url.searchParams.append('step', 'contact_information');
104
+
105
+ consoleSuccess('\nSuccessfully pushed the extension.');
106
+ consoleBlue(`Your extension's preview URL: ${url.toString()}`);
107
+ } catch (err) {
108
+ consoleError('\n Failed to push the extension: ', err);
109
+ throw 'push_failed';
110
+ } finally {
111
+ load.stop();
112
+ }
113
+ }
114
+
115
+ /**
116
+ * 初始化需要push的extension信息
117
+ */
118
+ async function initSelectedExtensionInfo() {
119
+ const PUSH_NEW_EXTENSION = 'Push new extension';
120
+ const res = await api.getExtensionList();
121
+ const extensionList = res.data?.data.extensions || [];
122
+ extensionList.unshift({ extension_id: PUSH_NEW_EXTENSION, name: 'Push new extension' });
123
+ let extensionInfo = await useSelectExtensionMode(extensionList);
124
+ if (extensionInfo.extension_id === PUSH_NEW_EXTENSION) {
125
+ const { extensionName } = await inquirer.prompt([
126
+ {
127
+ type: 'input',
128
+ name: 'extensionName',
129
+ message: 'Please enter the extension name:',
130
+ prefix: '*'
131
+ }
132
+ ]);
133
+ return {
134
+ name: extensionName
135
+ };
136
+ } else {
137
+ checkLocalExtension(extensionInfo);
138
+ return extensionInfo;
139
+ }
140
+ }
@@ -0,0 +1,34 @@
1
+ # Checkout Extension
2
+ ## Getting started
3
+
4
+ 1. 安装依赖
5
+
6
+ ```
7
+ npm i
8
+ ```
9
+ 2. 启动本地服务器
10
+ ```
11
+ shoplazza checkout dev
12
+ ```
13
+
14
+ ## lessJS组件库
15
+
16
+ 当自定义UI比较复杂时可以使用 lessJS组件库 https://lessjs.shoplazza.com/latest/docs/introduction/
17
+
18
+ lessJS已经被内置,无需引入,开箱即用。
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
@@ -0,0 +1,4 @@
1
+ node_modules/
2
+ pnpm-lock.yaml
3
+ dist
4
+ .history/
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ token: '{token}',
3
+ store: '{store}'
4
+ };
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": "1.0",
3
+ "deleteTarget": [],
4
+ "placeholder": {},
5
+ "templateName": "checkout",
6
+ "themeName": "",
7
+ "extensionId":"",
8
+ "extensionName":"",
9
+ "extensionDescription":""
10
+ }
@@ -0,0 +1,3 @@
1
+ <div>
2
+ <h2>hello world</h2>
3
+ </div>
@@ -0,0 +1,5 @@
1
+ <div>
2
+ import './style.html'
3
+ import './content.html'
4
+ import './script.html'
5
+ </div>
@@ -0,0 +1,11 @@
1
+ import { extend } from '@shoplazza/extension-ui';
2
+ import index from './index.html';
3
+
4
+ function App() {
5
+ return index;
6
+ }
7
+
8
+ extend({
9
+ extensionPoint: 'Checkout::Navigate::RenderBefore',
10
+ component: App()
11
+ });
@@ -0,0 +1,3 @@
1
+ <ljs-script layout="logic" type="application/javascript">
2
+ <!-- js代码 -->
3
+ </ljs-script>
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "chick-extension",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "dependencies": {
7
+ "fs-extra": "11.1.1",
8
+ "@shoplazza/extension-ui": "2.0.8"
9
+ },
10
+ "devDependencies": {},
11
+ "scripts": {
12
+ "test": "echo \"Error: no test specified\" && exit 1"
13
+ },
14
+ "keywords": [],
15
+ "author": "",
16
+ "license": "ISC"
17
+ }
@@ -0,0 +1,40 @@
1
+ const api = require('./api');
2
+ const { verifyConfig } = require('./verify');
3
+ const { useSelectExtensionMode } = require('./util');
4
+ const inquirer = require('inquirer');
5
+ const { consoleSuccess, consoleBlue, consoleError } = require('./console');
6
+
7
+ module.exports = async () => {
8
+ try {
9
+ await verifyConfig();
10
+ const extensionInfo = await initSelectedExtensionInfo();
11
+ await undeployExtension(extensionInfo);
12
+ } catch (error) {
13
+ error.response ? consoleError(`Error: ${error.response.status}-${error.response.config.url}`) : consoleError(error);
14
+ }
15
+ };
16
+
17
+ async function initSelectedExtensionInfo() {
18
+ const res = await api.getExtensionList({
19
+ status: 'published'
20
+ });
21
+ const extensionList = res.data?.data?.extensions || [];
22
+ return useSelectExtensionMode(extensionList, 'undeploy');
23
+ }
24
+
25
+ async function undeployExtension(extensionInfo) {
26
+ const { confirm } = await inquirer.prompt([
27
+ {
28
+ type: 'confirm',
29
+ name: 'confirm',
30
+ message: 'Are you sure you want to undeploy?',
31
+ default: false
32
+ }
33
+ ]);
34
+ if (confirm) {
35
+ await api.undeployExtension({ extension_id: extensionInfo.extension_id });
36
+ consoleSuccess(`Successfully undeployed the extension '${extensionInfo.name}'.`);
37
+ } else {
38
+ consoleBlue('Undeploy cancelled');
39
+ }
40
+ }