@jamesblanksby/toolkit 1.1.1 → 1.3.0

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/build/sass.js CHANGED
@@ -5,72 +5,60 @@ import * as sass from 'sass';
5
5
 
6
6
  import { MemoryFile } from './../src/file.js';
7
7
 
8
- let sassPath = null;
8
+ async function findMainPaths(source) {
9
+ const partialDir = path.dirname(source);
9
10
 
10
- async function findMainPath(partialPath) {
11
- const partialDir = path.dirname(partialPath);
12
-
13
- async function findMainPathRec(directory) {
11
+ async function findMainPathsRec(directory, matches = []) {
14
12
  const files = await fs.readdir(directory);
15
13
 
16
- const mainPath = files.find((file) => !file.startsWith('_') && file.endsWith('.scss'));
17
- if (mainPath) {
18
- return path.join(directory, mainPath);
19
- }
14
+ const matchedFiles = files.filter((file) => !file.startsWith('_') && file.endsWith('.scss'));
15
+ const fullPaths = matchedFiles.map((file) => path.join(directory, file));
16
+
17
+ matches.push(...fullPaths);
20
18
 
21
19
  const parentDir = path.dirname(directory);
22
20
  if (parentDir !== directory) {
23
- return findMainPathRec(parentDir);
21
+ return findMainPathsRec(parentDir, matches);
24
22
  }
25
23
 
26
- return null;
24
+ return matches;
27
25
  }
28
26
 
29
- return findMainPathRec(partialDir);
30
- }
31
-
32
- function createCssFile(css, target) {
33
- return new MemoryFile(target, css);
34
- }
35
-
36
- async function createMapFile(map, target) {
37
- const sassDir = path.resolve(sassPath, './../..');
38
-
39
- map.sources = map.sources.map((source) => source.replace(`file://${sassDir}`, '..'));
40
-
41
- await fs.writeFile(target, JSON.stringify(map));
27
+ return findMainPathsRec(partialDir);
42
28
  }
43
29
 
44
- async function createCssFileWithMap(file, source) {
45
- const mapName = path.basename(source);
30
+ async function createCssAndMapFile(css, map, source) {
31
+ const cssPath = source.replace(/sass|scss/g, 'css');
32
+ const mapPath = `${cssPath}.map`;
33
+ const mapName = path.basename(mapPath);
46
34
 
47
- file = await file.read();
48
-
49
- file = new MemoryFile(file.path, [
50
- file.contents,
35
+ const file = new MemoryFile(cssPath, [
36
+ css,
51
37
  `/*# sourceMappingURL=${mapName} */`,
52
38
  ].join('\n'));
53
39
 
40
+ const sassDir = path.resolve(source, './../..');
41
+ map.sources = map.sources.map((source) => source.replace(`file://${sassDir}`, '..'));
42
+
43
+ await fs.writeFile(mapPath, JSON.stringify(map));
44
+
54
45
  return file;
55
46
  }
56
47
 
48
+
57
49
  export default async function* sassCompile(files) {
58
50
  for await (const file of files) {
59
- sassPath = await findMainPath(file.path);
51
+ const sassPaths = await findMainPaths(file.path);
60
52
 
61
- let result;
62
- try {
63
- result = sass.compile(sassPath, { sourceMap: true, });
64
- } catch (error) {
65
- throw error.message;
66
- }
67
-
68
- const cssPath = sassPath.replace(/sass|scss/g, 'css');
69
- let cssFile = createCssFile(result.css, cssPath);
53
+ for (const sassPath of sassPaths) {
54
+ let result;
55
+ try {
56
+ result = sass.compile(sassPath, { sourceMap: true, });
57
+ } catch (error) {
58
+ throw new Error(error.message);
59
+ }
70
60
 
71
- const mapPath = `${cssPath}.map`;
72
- await createMapFile(result.sourceMap, mapPath);
73
-
74
- yield await createCssFileWithMap(cssFile, mapPath);
61
+ yield await createCssAndMapFile(result.css, result.sourceMap, sassPath);
62
+ }
75
63
  }
76
64
  }
package/build/shopify.js CHANGED
@@ -4,11 +4,12 @@ import uglifyjs from 'uglify-js';
4
4
 
5
5
  import { MemoryFile } from './../index.js';
6
6
 
7
- const { PWD, } = process.env;
8
-
9
7
  function flattenAsset(file, type) {
10
- const name = path.relative(PWD, file.path)
11
- .replace(new RegExp(`src/${type}/?`), '')
8
+ const assetDir = process.env.ASSET_DIR || process.env.PWD;
9
+
10
+ const name = path.relative(assetDir, file.path)
11
+ .replace(new RegExp(`src/?`), '')
12
+ .replace(new RegExp(`${type}/?`), '')
12
13
  .replace(new RegExp('/', 'g'), '_');
13
14
 
14
15
  return new MemoryFile(name, file.buffer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesblanksby/toolkit",
3
- "version": "1.1.1",
3
+ "version": "1.3.0",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
package/toolkit.config.js CHANGED
@@ -13,8 +13,8 @@ const { PWD, } = process.env;
13
13
  const pattern = {
14
14
  html: `${PWD}/**/*.{html,php}`,
15
15
  sass: `${PWD}/**/{sass,scss}/**/*.{sass,scss}`,
16
- css: `${PWD}/**/css/*.css`,
17
- script: `${PWD}/**/script/*.js`,
16
+ css: `${PWD}/**/css/**/*.css`,
17
+ script: `${PWD}/**/script/**/*.js`,
18
18
  };
19
19
 
20
20
  function sync() {
@@ -63,10 +63,11 @@ function minify() {
63
63
 
64
64
  async function shopify() {
65
65
  const shopifyDir = `${PWD}/../shopify/assets`;
66
+ const assetDir = process.env.ASSET_DIR || PWD;
66
67
 
67
68
  await src(`${shopifyDir}/**`).rm().toArray();
68
69
 
69
- return src(`${PWD}/**/(css|font|gfx|plugin|script)/**`, { ignore: '**/node_modules/**', })
70
+ return src(`${assetDir}/**/(css|font|gfx|plugin|script)/**`, { ignore: '**/node_modules/**', })
70
71
  .pipe(shopifyFlatten)
71
72
  .dest(shopifyDir);
72
73
  }