Merge pull request #5051 from solgenomics/topic/locus_owner
[sgn.git] / js / webpack_util / utils.js
blob46550b720f02bf2955f3556a0e1a74445763322c
1 const path = require('path');
2 const fs = require('fs');
4 const JSAN_adaptor = path.resolve(__dirname, 'adaptor.js');
5 const common_js_regex = /(?<=(?:[\r\n]+|^))\s*require *\( *(?:"(.*?)"|'(.*?)') *\) *;?\s*/g;
6 const import_regex = /(?<=(?:[\r\n]+|^))\s*import +(?:"(.*?)"|'(.*?)') *;?\s*/g;
8 // Converts source path to a JSAN-style string
9 function src_path_to_JSAN(legacy,src_path){
10 src_path = path.resolve(__dirname, src_path);
11 var relative_src_path = path.relative(legacy, src_path);
12 var JSAN_id = relative_src_path
13 .replace(/\.js$/,"")
14 .replace(/\//g,".")
15 .replace(/^\.*/,"");
16 return `JSAN.use("${JSAN_id}");\n`;
19 // Converts JSAN include to a source path
20 function JSAN_to_src_path(legacy,jsan){
21 var JSAN_path = jsan
22 .replace(/^JSAN\.use\(["']/,"")
23 .replace(/["']\);\s*$/,"")
24 .replace(/\./g,"/")
25 .replace(/(\.js)?$/,".js");
26 src_path = path.resolve(legacy, JSAN_path);
27 return src_path;
30 // Checks if one path is contained in another
31 function isChildOf(child, parent){
32 child = path.resolve(__dirname, child);
33 parent = path.resolve(__dirname, parent);
34 if (child === parent) return false
35 const parentTokens = parent.split('/').filter(i => i.length)
36 const childTokens = child.split('/').filter(i => i.length)
37 return parentTokens.every((t, i) => childTokens[i] === t)
40 module.exports = {
41 'JSAN_adaptor':JSAN_adaptor,
42 'common_js_regex':common_js_regex,
43 'import_regex':import_regex,
44 'src_path_to_JSAN':src_path_to_JSAN,
45 'JSAN_to_src_path':JSAN_to_src_path,
46 'isChildOf':isChildOf