@lowdefy/node-utils 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,19 @@
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 fsExtra from 'fs-extra';
16
+ async function cleanDirectory(dirPath) {
17
+ await fsExtra.emptyDir(dirPath);
18
+ }
19
+ export default cleanDirectory;
@@ -0,0 +1,19 @@
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 fsExtra from 'fs-extra';
16
+ async function copyDirectory(dirPathFrom, dirPathTo, options) {
17
+ await fsExtra.copy(dirPathFrom, dirPathTo, options);
18
+ }
19
+ export default copyDirectory;
@@ -0,0 +1,24 @@
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 getConfigFromEnv() {
16
+ return {
17
+ buildDirectory: process.env.LOWDEFY_SERVER_BUILD_DIRECTORY,
18
+ logLevel: process.env.LOWDEFY_SERVER_LOG_LEVEL,
19
+ publicDirectory: process.env.LOWDEFY_SERVER_PUBLIC_DIRECTORY,
20
+ port: process.env.LOWDEFY_SERVER_PORT && parseInt(process.env.LOWDEFY_SERVER_PORT),
21
+ basePath: process.env.LOWDEFY_BASE_PATH
22
+ };
23
+ }
24
+ export default getConfigFromEnv;
@@ -0,0 +1,30 @@
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 getFileExtension(filePath) {
16
+ const arr = filePath.split('.');
17
+ if (arr.length === 1) {
18
+ return null;
19
+ }
20
+ return arr[arr.length - 1];
21
+ }
22
+ function getFileSubExtension(path) {
23
+ const arr = path.split('.');
24
+ if (arr.length < 3) {
25
+ return null;
26
+ }
27
+ return arr[arr.length - 2];
28
+ }
29
+ export { getFileSubExtension };
30
+ export default getFileExtension;
@@ -0,0 +1,25 @@
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 getSecretsFromEnv() {
16
+ const secrets = {};
17
+ Object.keys(process.env).forEach((key)=>{
18
+ if (key.startsWith('LOWDEFY_SECRET_')) {
19
+ secrets[key.replace('LOWDEFY_SECRET_', '')] = process.env[key];
20
+ }
21
+ });
22
+ Object.freeze(secrets);
23
+ return secrets;
24
+ }
25
+ export default getSecretsFromEnv;
package/dist/index.js ADDED
@@ -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 cleanDirectory from './cleanDirectory.js';
16
+ import copyDirectory from './copyDirectory.js';
17
+ import getConfigFromEnv from './getConfigFromEnv.js';
18
+ import getFileExtension, { getFileSubExtension } from './getFileExtension.js';
19
+ import getSecretsFromEnv from './getSecretsFromEnv.js';
20
+ import spawnProcess from './spawnProcess.js';
21
+ import readFile from './readFile.js';
22
+ import writeFile from './writeFile.js';
23
+ export { cleanDirectory, copyDirectory, getConfigFromEnv, getFileExtension, getFileSubExtension, getSecretsFromEnv, spawnProcess, readFile, writeFile };
@@ -0,0 +1,35 @@
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 { promisify } from 'util';
18
+ import { type } from '@lowdefy/helpers';
19
+ const readFilePromise = promisify(fs.readFile);
20
+ async function readFile(filePath) {
21
+ if (!type.isString(filePath)) {
22
+ throw new Error(`Could not read file, file path should be a string, received ${JSON.stringify(filePath)}.`);
23
+ }
24
+ try {
25
+ // By specifying encoding, readFile returns a string instead of a buffer.
26
+ const file = await readFilePromise(path.resolve(filePath), 'utf8');
27
+ return file;
28
+ } catch (error) {
29
+ if (error.code === 'ENOENT' || error.code === 'EISDIR') {
30
+ return null;
31
+ }
32
+ throw error;
33
+ }
34
+ }
35
+ export default readFile;
@@ -0,0 +1,52 @@
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 { spawn } from 'child_process';
16
+ function createStdIOHandler({ lineHandler }) {
17
+ function handler(data) {
18
+ data.toString('utf8').split('\n').forEach((line)=>{
19
+ if (line) {
20
+ lineHandler(line);
21
+ }
22
+ });
23
+ }
24
+ return handler;
25
+ }
26
+ function spawnProcess({ args , command , processOptions , returnProcess , stdErrLineHandler , stdOutLineHandler =()=>{} , }) {
27
+ if (!stdErrLineHandler) {
28
+ stdErrLineHandler = stdOutLineHandler;
29
+ }
30
+ const process = spawn(command, args, processOptions);
31
+ process.stdout.on('data', createStdIOHandler({
32
+ lineHandler: stdOutLineHandler
33
+ }));
34
+ process.stderr.on('data', createStdIOHandler({
35
+ lineHandler: stdErrLineHandler
36
+ }));
37
+ if (returnProcess) {
38
+ return process;
39
+ }
40
+ return new Promise((resolve, reject)=>{
41
+ process.on('error', (error)=>{
42
+ stdErrLineHandler(error);
43
+ });
44
+ process.on('exit', (code)=>{
45
+ if (code !== 0) {
46
+ reject(new Error(`${command} exited with code ${code}`));
47
+ }
48
+ resolve();
49
+ });
50
+ });
51
+ }
52
+ export default spawnProcess;
@@ -0,0 +1,38 @@
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 { promisify } from 'util';
18
+ import { type } from '@lowdefy/helpers';
19
+ const mkdirPromise = promisify(fs.mkdir);
20
+ const writeFilePromise = promisify(fs.writeFile);
21
+ async function writeFile(filePath, content) {
22
+ if (!type.isString(filePath)) {
23
+ throw new Error(`Could not write file, file path should be a string, received ${JSON.stringify(filePath)}.`);
24
+ }
25
+ try {
26
+ await writeFilePromise(path.resolve(filePath), content);
27
+ } catch (error) {
28
+ if (error.code === 'ENOENT') {
29
+ await mkdirPromise(path.dirname(filePath), {
30
+ recursive: true
31
+ });
32
+ await writeFilePromise(filePath, content);
33
+ return;
34
+ }
35
+ throw error;
36
+ }
37
+ }
38
+ export default writeFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/node-utils",
3
- "version": "4.0.0-alpha.29",
3
+ "version": "4.0.0-alpha.31",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -40,7 +40,7 @@
40
40
  "test": "jest --coverage"
41
41
  },
42
42
  "dependencies": {
43
- "@lowdefy/helpers": "4.0.0-alpha.29",
43
+ "@lowdefy/helpers": "4.0.0-alpha.31",
44
44
  "fs-extra": "10.1.0"
45
45
  },
46
46
  "devDependencies": {
@@ -52,5 +52,5 @@
52
52
  "publishConfig": {
53
53
  "access": "public"
54
54
  },
55
- "gitHead": "621a191ebc0a1569ee6669dc74c12f8be5a8c7f3"
55
+ "gitHead": "96ef86d4ce4849f8f11110662efbbaede1bcd5a5"
56
56
  }