lowdefy 4.0.0-alpha.29 → 4.0.0-alpha.31

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.
@@ -0,0 +1,26 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ function createStdOutLineHandler({ context }) {
16
+ function stdOutLineHandler(line) {
17
+ try {
18
+ const { print , msg } = JSON.parse(line);
19
+ context.print[print](msg);
20
+ } catch (error) {
21
+ context.print.log(line);
22
+ }
23
+ }
24
+ return stdOutLineHandler;
25
+ }
26
+ export default createStdOutLineHandler;
@@ -0,0 +1,51 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import axios from 'axios';
16
+ import createPrint from './createPrint.js';
17
+ async function logError({ error , context ={} }) {
18
+ try {
19
+ await axios.request({
20
+ method: 'post',
21
+ url: 'https://api.lowdefy.net/errors',
22
+ headers: {
23
+ 'User-Agent': `Lowdefy CLI v${context.cliVersion}`
24
+ },
25
+ data: {
26
+ source: 'cli',
27
+ cliVersion: context.cliVersion,
28
+ command: context.command,
29
+ lowdefyVersion: context.lowdefyVersion,
30
+ message: error.message,
31
+ name: error.name,
32
+ stack: error.stack
33
+ }
34
+ });
35
+ } catch (error1) {
36
+ // pass
37
+ }
38
+ }
39
+ async function errorHandler({ context , error }) {
40
+ const print = createPrint({
41
+ logLevel: 'info'
42
+ });
43
+ print.error(error.message);
44
+ if (!context.disableTelemetry) {
45
+ await logError({
46
+ context,
47
+ error
48
+ });
49
+ }
50
+ }
51
+ export default errorHandler;
@@ -0,0 +1,56 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import axios from 'axios';
16
+ import decompress from 'decompress';
17
+ import decompressTargz from 'decompress-targz';
18
+ async function fetchNpmTarball({ packageName , version , directory }) {
19
+ const registryUrl = `https://registry.npmjs.org/${packageName}`;
20
+ let packageInfo;
21
+ try {
22
+ packageInfo = await axios.get(registryUrl);
23
+ } catch (error) {
24
+ if (error.response && error.response.status === 404) {
25
+ throw new Error(`Package "${packageName}" could not be found at ${registryUrl}.`);
26
+ }
27
+ throw error;
28
+ }
29
+ if (!packageInfo || !packageInfo.data) {
30
+ throw new Error(`Package "${packageName}" could not be found at ${registryUrl}.`);
31
+ }
32
+ if (!packageInfo.data.versions[version]) {
33
+ throw new Error(`Invalid version. "${packageName}" does not have version "${version}".`);
34
+ }
35
+ let tarball;
36
+ try {
37
+ tarball = await axios.get(packageInfo.data.versions[version].dist.tarball, {
38
+ responseType: 'arraybuffer'
39
+ });
40
+ } catch (error1) {
41
+ if (error1.response && error1.response.status === 404) {
42
+ throw new Error(`Package "${packageName}" tarball could not be found at ${packageInfo.data.versions[version].dist.tarball}.`);
43
+ }
44
+ throw error1;
45
+ }
46
+ if (!tarball || !tarball.data) {
47
+ throw new Error(`Package "${packageName}" tarball could not be found at ${packageInfo.data.versions[version].dist.tarball}.`);
48
+ }
49
+ await decompress(tarball.data, directory, {
50
+ plugins: [
51
+ decompressTargz()
52
+ ],
53
+ strip: 1
54
+ });
55
+ }
56
+ export default fetchNpmTarball;
@@ -0,0 +1,58 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ // Based on: https://github.com/testiumjs/find-open-port
16
+ /*
17
+ * Copyright (c) 2015, Groupon, Inc.
18
+ * All rights reserved.
19
+ *
20
+ * Redistribution and use in source and binary forms, with or without
21
+ * modification, are permitted provided that the following conditions
22
+ * are met:
23
+ *
24
+ * Redistributions of source code must retain the above copyright notice,
25
+ * this list of conditions and the following disclaimer.
26
+ *
27
+ * Redistributions in binary form must reproduce the above copyright
28
+ * notice, this list of conditions and the following disclaimer in the
29
+ * documentation and/or other materials provided with the distribution.
30
+ *
31
+ * Neither the name of GROUPON nor the names of its contributors may be
32
+ * used to endorse or promote products derived from this software without
33
+ * specific prior written permission.
34
+ *
35
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
36
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
+ */ import net from 'net';
47
+ async function findPort() {
48
+ return new Promise((resolve, reject)=>{
49
+ const server = net.createServer();
50
+ server.on('error', reject);
51
+ server.listen(0, ()=>{
52
+ const { port } = server.address();
53
+ server.on('close', resolve.bind(null, port));
54
+ server.close();
55
+ });
56
+ });
57
+ }
58
+ export default findPort;
@@ -0,0 +1,32 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import path from 'path';
16
+ import { readFile, writeFile } from '@lowdefy/node-utils';
17
+ import { v4 as uuid } from 'uuid';
18
+ async function getCliJson({ configDirectory }) {
19
+ const filePath = path.resolve(configDirectory, './.lowdefy/cli.json');
20
+ const cliJson = await readFile(filePath);
21
+ if (!cliJson) {
22
+ const appId = uuid();
23
+ await writeFile(filePath, JSON.stringify({
24
+ appId
25
+ }, null, 2));
26
+ return {
27
+ appId
28
+ };
29
+ }
30
+ return JSON.parse(cliJson);
31
+ }
32
+ export default getCliJson;
@@ -0,0 +1,28 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import path from 'path';
16
+ function getDirectories({ configDirectory , options }) {
17
+ const dotLowdefy = path.resolve(configDirectory, '.lowdefy');
18
+ const server = options.serverDirectory ? path.resolve(options.serverDirectory) : path.join(dotLowdefy, 'server');
19
+ // TODO: Read server directory from env var
20
+ // Priority should be CLI arguments, env var, CLI options in Lowdefy config
21
+ return {
22
+ config: configDirectory,
23
+ build: path.join(server, 'build'),
24
+ server,
25
+ dev: options.devDirectory ? path.resolve(options.devDirectory) : path.join(dotLowdefy, 'dev')
26
+ };
27
+ }
28
+ export default getDirectories;
@@ -0,0 +1,57 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import path from 'path';
16
+ import { get, type } from '@lowdefy/helpers';
17
+ import { readFile } from '@lowdefy/node-utils';
18
+ import YAML from 'yaml';
19
+ async function getLowdefyYaml({ configDirectory , command }) {
20
+ let lowdefyYaml = await readFile(path.join(configDirectory, 'lowdefy.yaml'));
21
+ if (!lowdefyYaml) {
22
+ lowdefyYaml = await readFile(path.join(configDirectory, 'lowdefy.yml'));
23
+ }
24
+ if (!lowdefyYaml) {
25
+ if (![
26
+ 'init'
27
+ ].includes(command)) {
28
+ throw new Error(`Could not find "lowdefy.yaml" file in specified config directory ${configDirectory}.`);
29
+ }
30
+ return {
31
+ cliConfig: {}
32
+ };
33
+ }
34
+ let lowdefy;
35
+ try {
36
+ lowdefy = YAML.parse(lowdefyYaml);
37
+ } catch (error) {
38
+ throw new Error(`Could not parse "lowdefy.yaml" file. Received error ${error.message}.`);
39
+ }
40
+ if (!lowdefy.lowdefy) {
41
+ throw new Error(`No version specified in "lowdefy.yaml" file. Specify a version in the "lowdefy" field.`);
42
+ }
43
+ if (!type.isString(lowdefy.lowdefy)) {
44
+ throw new Error(`Version number specified in "lowdefy.yaml" file should be a string. Received ${JSON.stringify(lowdefy.lowdefy)}.`);
45
+ }
46
+ // TODO: Validate plugins
47
+ return {
48
+ lowdefyVersion: lowdefy.lowdefy,
49
+ cliConfig: get(lowdefy, 'cli', {
50
+ default: {}
51
+ }),
52
+ plugins: get(lowdefy, 'plugins', {
53
+ default: []
54
+ })
55
+ };
56
+ }
57
+ export default getLowdefyYaml;
@@ -0,0 +1,23 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ function getOptions({ commandLineOptions , cliConfig }) {
16
+ // commandLineOptions take precedence over config in lowdefy.yaml
17
+ const options = {
18
+ ...cliConfig,
19
+ ...commandLineOptions
20
+ };
21
+ return options;
22
+ }
23
+ export default getOptions;
@@ -0,0 +1,54 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import path from 'path';
16
+ import axios from 'axios';
17
+ import { readFile } from '@lowdefy/node-utils';
18
+ async function getTypes({ directories }) {
19
+ return JSON.parse(await readFile(path.join(directories.build, 'types.json')));
20
+ }
21
+ function getSendTelemetry({ appId , cliVersion , command , directories , lowdefyVersion , options }) {
22
+ if (options.disableTelemetry) {
23
+ return ()=>{};
24
+ }
25
+ async function sendTelemetry({ data ={} , sendTypes =false } = {}) {
26
+ let types;
27
+ if (sendTypes) {
28
+ types = await getTypes({
29
+ directories
30
+ });
31
+ }
32
+ try {
33
+ await axios.request({
34
+ method: 'post',
35
+ url: 'https://api.lowdefy.net/telemetry/cli',
36
+ headers: {
37
+ 'User-Agent': `Lowdefy CLI v${cliVersion}`
38
+ },
39
+ data: {
40
+ ...data,
41
+ app_id: appId,
42
+ cli_version: cliVersion,
43
+ command,
44
+ lowdefy_version: lowdefyVersion,
45
+ types
46
+ }
47
+ });
48
+ } catch (error) {
49
+ // pass
50
+ }
51
+ }
52
+ return sendTelemetry;
53
+ }
54
+ export default getSendTelemetry;
@@ -0,0 +1,45 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import fs from 'fs';
16
+ import path from 'path';
17
+ import { cleanDirectory, readFile } from '@lowdefy/node-utils';
18
+ import fetchNpmTarball from './fetchNpmTarball.js';
19
+ async function getServer({ context , packageName , directory }) {
20
+ if (context.lowdefyVersion === 'local') {
21
+ context.print.warn(`Running local ${packageName}.`);
22
+ return;
23
+ }
24
+ let fetchServer = false;
25
+ const serverExists = fs.existsSync(path.join(directory, 'package.json'));
26
+ if (!serverExists) fetchServer = true;
27
+ if (serverExists) {
28
+ const serverPackageConfig = JSON.parse(await readFile(path.join(directory, 'package.json')));
29
+ if (serverPackageConfig.version !== context.lowdefyVersion) {
30
+ fetchServer = true;
31
+ context.print.warn(`Removing ${packageName} with version ${serverPackageConfig.version}`);
32
+ await cleanDirectory(directory);
33
+ }
34
+ }
35
+ if (fetchServer) {
36
+ context.print.spin(`Fetching ${packageName} from npm.`);
37
+ await fetchNpmTarball({
38
+ packageName,
39
+ version: context.lowdefyVersion,
40
+ directory: directory
41
+ });
42
+ context.print.log(`Fetched ${packageName} from npm.`);
43
+ }
44
+ }
45
+ export default getServer;
@@ -0,0 +1,36 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { spawnProcess } from '@lowdefy/node-utils';
16
+ async function installServer({ context , directory }) {
17
+ context.print.spin('Installing dependencies.');
18
+ try {
19
+ await spawnProcess({
20
+ command: context.pnpmCmd,
21
+ args: [
22
+ 'install',
23
+ '--no-frozen-lockfile'
24
+ ],
25
+ stdOutLineHandler: (line)=>context.print.debug(line),
26
+ processOptions: {
27
+ cwd: directory
28
+ }
29
+ });
30
+ } catch (error) {
31
+ console.log(error);
32
+ throw new Error('Dependency installation failed.');
33
+ }
34
+ context.print.log('Dependencies install successfully.');
35
+ }
36
+ export default installServer;
@@ -0,0 +1,23 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import path from 'path';
16
+ import dotenv from 'dotenv';
17
+ function readDotEnv(context) {
18
+ dotenv.config({
19
+ path: path.join(context.directories.config, '.env'),
20
+ silent: true
21
+ });
22
+ }
23
+ export default readDotEnv;
@@ -0,0 +1,41 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import errorHandler from './errorHandler.js';
16
+ import startUp from './startUp.js';
17
+ const runCommand = ({ cliVersion , handler })=>{
18
+ async function run(options, command) {
19
+ const context = {
20
+ cliVersion
21
+ };
22
+ try {
23
+ await startUp({
24
+ context,
25
+ options,
26
+ command
27
+ });
28
+ const res = await handler({
29
+ context
30
+ });
31
+ return res;
32
+ } catch (error) {
33
+ await errorHandler({
34
+ context,
35
+ error
36
+ });
37
+ }
38
+ }
39
+ return run;
40
+ };
41
+ export default runCommand;
@@ -0,0 +1,44 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { spawnProcess } from '@lowdefy/node-utils';
16
+ import createStdOutLineHandler from './createStdOutLineHandler.js';
17
+ async function runLowdefyBuild({ context , directory }) {
18
+ context.print.spin('Running Lowdefy build.');
19
+ try {
20
+ await spawnProcess({
21
+ args: [
22
+ 'run',
23
+ 'build:lowdefy'
24
+ ],
25
+ command: context.pnpmCmd,
26
+ stdOutLineHandler: createStdOutLineHandler({
27
+ context
28
+ }),
29
+ processOptions: {
30
+ cwd: directory,
31
+ env: {
32
+ ...process.env,
33
+ LOWDEFY_BUILD_REF_RESOLVER: context.options.refResolver,
34
+ LOWDEFY_DIRECTORY_CONFIG: context.directories.config,
35
+ LOWDEFY_LOG_LEVEL: context.options.logLevel
36
+ }
37
+ }
38
+ });
39
+ } catch (error) {
40
+ throw new Error('Lowdefy build failed.');
41
+ }
42
+ context.print.log('Lowdefy build successful.');
43
+ }
44
+ export default runLowdefyBuild;
@@ -0,0 +1,48 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { spawnProcess } from '@lowdefy/node-utils';
16
+ function createStdOutLineHandler({ context }) {
17
+ function stdOutLineHandler(line) {
18
+ // Matches next build output of form: ┌ λ / 261 B 403 kB
19
+ const match = line.match(/┌ λ \/\s*\d* [a-zA-Z]*\s*(\d* [a-zA-Z]*)/);
20
+ if (match) {
21
+ context.print.info(`Home page first load JS size: ${match[1]}.`);
22
+ }
23
+ context.print.debug(line);
24
+ }
25
+ return stdOutLineHandler;
26
+ }
27
+ async function runNextBuild({ context , directory }) {
28
+ context.print.spin('Running Next build.');
29
+ try {
30
+ await spawnProcess({
31
+ command: context.pnpmCmd,
32
+ args: [
33
+ 'run',
34
+ 'build:next'
35
+ ],
36
+ stdOutLineHandler: createStdOutLineHandler({
37
+ context
38
+ }),
39
+ processOptions: {
40
+ cwd: directory
41
+ }
42
+ });
43
+ } catch (error) {
44
+ throw new Error('Next build failed.');
45
+ }
46
+ context.print.log('Next build successful.');
47
+ }
48
+ export default runNextBuild;