|
| 1 | +var fs = require('fs-extra'); |
| 2 | +var path = require('path'); |
| 3 | +var cheerio = require('cheerio'); |
| 4 | + |
| 5 | +// get the root directory; |
| 6 | +var rootDirectory = path.dirname(__dirname); |
| 7 | +// create a meaningful dirname add "-for-editor" to indicate |
| 8 | +var codePackageCopyName = rootDirectory.split("/").slice(-1).pop() + "-for-editor"; |
| 9 | +// create a path to the parent directory of the root |
| 10 | +var copyPathDirectory = path.join(__dirname, '../../', codePackageCopyName) |
| 11 | + |
| 12 | +console.log(rootDirectory) |
| 13 | +console.log(copyPathDirectory) |
| 14 | + |
| 15 | +// check if the folder exists, if so remove it! |
| 16 | +// NOTE: any changes to that dir will be removed! |
| 17 | +// So be sure to always write changes to the main repo |
| 18 | +if(fs.existsSync(copyPathDirectory)){ |
| 19 | + fs.removeSync(copyPathDirectory) |
| 20 | +} |
| 21 | + |
| 22 | +/* |
| 23 | +Here's where the magic happens |
| 24 | +*/ |
| 25 | + |
| 26 | +// first make of copy of the sketches |
| 27 | +copyFolderContents(rootDirectory, copyPathDirectory) |
| 28 | + |
| 29 | +// then get the directories for the 01_P and 02_M sketches |
| 30 | +var pDirs = getDirectories(copyPathDirectory+"/01_P/").map(function(dir){return copyPathDirectory +"/01_P/" + dir}); |
| 31 | +var pNewDirs = getDirectories(copyPathDirectory+"/01_P_NEW/").map(function(dir){return copyPathDirectory +"/01_P_NEW/" + dir}); |
| 32 | +var mDirs = getDirectories(copyPathDirectory+"/02_M/").map(function(dir){return copyPathDirectory +"/02_M/" + dir}); |
| 33 | + |
| 34 | +// copy the libs and fix the paths |
| 35 | +make(copyPathDirectory, pDirs) |
| 36 | +make(copyPathDirectory, pNewDirs) |
| 37 | +make(copyPathDirectory, mDirs) |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +/* |
| 42 | +@@@ make |
| 43 | +Put it all together, Run through each sketch and |
| 44 | +copy over the dependencies |
| 45 | +*/ |
| 46 | +function make(newCodePackagePath,sketches){ |
| 47 | + sketches.forEach(function(dir){ |
| 48 | + if(dir.endsWith("_footage") == false){ |
| 49 | + |
| 50 | + if(fs.existsSync(dir+'/index.html')){ |
| 51 | + var dirIndex = dir+'/index.html' |
| 52 | + var sketchDeps = getSketchDependencies(dirIndex); |
| 53 | + copyDependencies(newCodePackagePath, sketchDeps, dir) |
| 54 | + } else{ |
| 55 | + console.log("no index file for: " + dir) |
| 56 | + } |
| 57 | + } |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +/* |
| 63 | +@@@ getSketchDependencies |
| 64 | +get the js & css dependencies based on the index.html file |
| 65 | +*/ |
| 66 | +function getSketchDependencies(path){ |
| 67 | + var indexFile = fs.readFileSync(path, "utf8"); |
| 68 | + |
| 69 | + var reqSrcs = []; |
| 70 | + |
| 71 | + $ = cheerio.load(indexFile); |
| 72 | + |
| 73 | + // get the script tags |
| 74 | + $('script').each(function(i, elem) { |
| 75 | + var libsrc = $(elem)[0].attribs.src; |
| 76 | + |
| 77 | + if(libsrc !== undefined ){ |
| 78 | + if(libsrc.split("/")[0] == ".."){ |
| 79 | + libsrc = libsrc.split("/").slice(2).join("/"); |
| 80 | + reqSrcs.push(libsrc); |
| 81 | + } |
| 82 | + } |
| 83 | + $(this).attr("src", libsrc); |
| 84 | + }) |
| 85 | + |
| 86 | + // get the links |
| 87 | + $('link').each(function(i, elem) { |
| 88 | + var libhref = $(elem)[0].attribs.href; |
| 89 | + libhref = libhref.split("/").slice(2).join("/"); |
| 90 | + reqSrcs.push(libhref); |
| 91 | + $(this).attr("href", libhref); |
| 92 | + }) |
| 93 | + |
| 94 | + // console.log($.html()) |
| 95 | + // console.log(reqSrcs) |
| 96 | + // write out the file |
| 97 | + fs.writeFileSync(path, $.html()) |
| 98 | + |
| 99 | + return reqSrcs; |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/* |
| 105 | +@@@ copyDependencies |
| 106 | +copy over all the dependencies in a loop |
| 107 | +feeding in the lib array and the current sketch |
| 108 | +*/ |
| 109 | +function copyDependencies(newCodePackagePath,myLibs, currentSketch){ |
| 110 | + // copyFolderContents(codePackageCopyPath + sketchDeps[0], pDirs[0] + "/" + sketchDeps[0]) |
| 111 | + myLibs.forEach(function(lib){ |
| 112 | + copyFolderContents(newCodePackagePath+"/"+lib, currentSketch + "/" + lib); |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +/* |
| 117 | +@@@ getDirectories |
| 118 | +return the path names |
| 119 | +*/ |
| 120 | +function getDirectories(path) { |
| 121 | + return fs.readdirSync(path).filter(function (file) { |
| 122 | + return fs.statSync(path+'/'+file).isDirectory(); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +/* |
| 127 | +@@@ copyFolderContents |
| 128 | +copy the folder contents |
| 129 | +*/ |
| 130 | +function copyFolderContents(fromFolder, toFolder){ |
| 131 | + // Sync: |
| 132 | + try { |
| 133 | + fs.copySync(fromFolder, toFolder, {overwrite: true}) |
| 134 | + // console.log('success!') |
| 135 | + } catch (err) { |
| 136 | + console.error(err) |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments