How to use zipDir method in redwood

Best JavaScript code snippet using redwood

core.js

Source:core.js Github

copy

Full Screen

1var cheerio = require('cheerio');2var path = require('path');3var fs = require('fs-extra');4var url = require('url');5var http = require('http');6var request = require('request');7var zipdir = {8 js: 'js',9 css: 'css',10 img: 'images'11};12var reg_http = /^(\s+)?(http(s)?\:)?\/\//;13var reg_cssurl = /url\s*\((\s*[A-Za-z0-9\-\_\.\/\:\"\']+\s*)\)\s*;?/gi;14// util funcs15function mkZipDirs (htmlPath) {16 var ret = {};17 var basedir = path.dirname(htmlPath);18 for (var k in zipdir) {19 var dir = path.join(basedir, zipdir[k]);20 fs.mkdirpSync(dir);21 ret[k] = dir;22 }23 return ret;24}25var uniqueId = function () {26 var i = 0;27 return function () {28 return i ++;29 }30}();31var Pack = function (opt) {32 //todo33 this._need = 0;34 this._done = 0;35 this._ev = {};36};37Pack.prototype = {38 on: function (ev, cb) {39 if (this._ev[ev] == undefined) this._ev[ev] = [];40 this._ev[ev].push(cb);41 },42 fire: function (ev) {43 var args = [].slice.call(arguments, 0);44 args.splice(0, 1);45 var cbs = this._ev[ev];46 if (cbs && cbs.length) {47 cbs.forEach(function (f) {48 f.apply(null, args)49 })50 }51 },52 /**53 * 处理htmlstring中的 img, script, link 标签引用的资源54 * @param {String} htmlstr 55 * @return {String} 处理好的htmlstring56 */57 parseHtml: function (htmlstr, filePath) {58 var me = this;59 if (!this.zipdir) {60 this.zipdir = mkZipDirs(filePath);61 }62 var $ = cheerio.load(htmlstr, {decodeEntities: false, normalizeWhitespace: false});63 $('link,script,img').each(function (i, el) {64 var $this = $(this);65 var tagName = $this.get(0).name;66 var href = tagName === 'link' ? $this.attr('href') : $this.attr('src');67 var attrs = {68 link: 'href',69 script: 'src',70 img: 'src'71 };72 if (reg_http.test(href)) {73 var absDestPath = me.saveFile(href);74 var relaPath = path.relative(path.dirname(filePath), absDestPath);75 $this.attr(attrs[tagName], relaPath);76 }77 });78 // 处理内联style标签79 $('style').each(function (i, el) {80 var $this = $(this);81 var cssstr = $this.html();82 var newCss = me.parseUriInCss(cssstr, filePath);83 $this.html(newCss);84 })85 return $.html();86 },87 parseUriInJs: function (jsstr, filePath) {88 jsstr = this.parseJsLoader(jsstr, filePath);89 //more ...90 //91 return jsstr;92 },93 parseUriInCss: function (cssstr, filePath, uri) {94 var me = this;95 if (!this.zipdir) {96 this.zipdir = mkZipDirs(filePath);97 }98 if (uri) {99 // 如果是在线down下来的css, 里面内容即使是相对路径的引用的资源,也要当作在线的处理100 101 }102 // 处理css中引用的在线资源103 cssstr = cssstr.replace(reg_cssurl, function (a, b) {104 if (reg_http.test(b)) {105 var destFile = me.saveFile(b);106 var relaPath = path.relative(path.dirname(filePath), destFile);107 return 'url('+ relaPath +')';108 }109 return a;110 });111 return cssstr;112 },113 parseJsLoader: function (jsstr, filePath) {114 //to be rewrited115 return jsstr;116 },117 saveFile: function (uri) {118 var me = this;119 this._need ++;120 if (!this.zipdir) {121 this.zipdir = mkZipDirs('');122 }123 function _dosave (dest, cb) {124 fs.ensureFileSync(dest);125 126 if (/^\/\//.test(uri)) uri = 'http:' + uri;127 request(uri)128 .on('end', function () {129 me._done ++;130 cb && cb();131 clearTimeout(me._timer);132 me._timer = setTimeout(function () {133 if (me._need === me._done) {134 me.fire('complete');135 }136 }, 1000);137 }).pipe(fs.createWriteStream(dest));138 }139 if (/\?\?/.test(uri)) {140 // cdn combo url141 var pathes = uri.split('??');142 var file0 = pathes[1].split(',')[0];143 var filetype = file0.substr(file0.lastIndexOf('.')+1, 999);144 var destPath = path.join(this.zipdir[filetype], 'cdncombo_' + uniqueId() + '.' + filetype);145 } else {146 var pathname = url.parse(uri).pathname;147 var filetype = pathname.split('.').pop();148 var destPath = path.join((this.zipdir[filetype] || this.zipdir['img']), pathname);149 }150 // 离线化后根据文件类型递归进行, 图片等资源不用做二次处理151 _dosave(destPath, function () {152 if (filetype === 'js') {153 var jscon = fs.readFileSync(destPath, {encoding:'utf8'});154 var newcon = me.parseUriInJs(jscon, destPath);155 newcon = me.parseUriInCss(newcon, destPath);156 fs.writeFileSync(destPath, newcon, {encoding:'utf8'});157 } else if (filetype === 'css') {158 var csscon = fs.readFileSync(destPath, {encoding:'utf8'});159 var newcon = me.parseUriInCss(csscon, destPath, uri);160 fs.writeFileSync(destPath, newcon, {encoding:'utf8'});161 }162 163 });164 return destPath;165 }166};...

Full Screen

Full Screen

motd.js

Source:motd.js Github

copy

Full Screen

1#!/home/gjones/mybin/jshost2System.include("Util.js");3System.include("Cod2Gen.js");4function motd()5{6 this.main = function() {7 var dt = new Date();8 var map_name;9 //dt.setDate(dt.getDate() + 1); // to do tomorrow, or later10 map_name = "mp_" + dt.getFullYear() + Util.padNumber(dt.getMonth()+1, 2, '0') + Util.padNumber(dt.getDate(), 2, '0');11 System.out.write("Creating map of the day: " + map_name + "\n");12 Cod2Gen.create("cod2.xml", map_name);13 System.out.write("Clearing zipdir contents.\n");14 System.execute("rm zipdir/mp/*");15 System.execute("rm zipdir/maps/mp/*");16 System.out.write("Moving gsc and arena files to zipdir.\n");17 System.execute("cp " + map_name + ".map zipdir/mp"); // not necessary, but nice18 System.execute("mv " + map_name + ".arena zipdir/mp");19 System.execute("mv " + map_name + ".gsc zipdir/maps/mp");20 System.out.write("Zipping up zipdir.\n");21 System.execute("cd zipdir; zip -r " + map_name + " *");22 23 System.out.write("Moving " + map_name + ".zip to current directory\n");24 System.execute("mv zipdir/" + map_name + ".zip .");25 System.out.write("Done!\n");26 }...

Full Screen

Full Screen

zip.ts

Source:zip.ts Github

copy

Full Screen

1const fs = require("fs");2const path = require("path");3const archiver = require("archiver");4const unzip = require("unzipper");5export const zipTo = (dir: string, zipDir: string) => {6 fs.mkdirSync(path.dirname(zipDir), { recursive: true });7 return new Promise(resolve => {8 try {9 fs.unlinkSync(zipDir);10 } catch (error) {}11 const output = fs.createWriteStream(zipDir);12 const archive = archiver("zip", {13 zlib: { level: 9 }14 });15 archive.on("end", () => {16 resolve(zipDir);17 });18 archive.pipe(output);19 archive.directory(dir, false);20 archive.finalize();21 });22};23export const zipFrom = (zipDir: string, dir: string) => {24 fs.mkdirSync(path.dirname(dir), { recursive: true });25 return new Promise((resolve, reject) => {26 fs.createReadStream(zipDir)27 .pipe(unzip.Extract({ path: dir }))28 .on("finish", resolve)29 .on("error", reject);30 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { zipDir } from '@redwoodjs/api'2export const handler = async (event, context) => {3 const zip = await zipDir('/tmp')4 return {5 headers: {6 },7 body: zip.toString('base64'),8 }9}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { zipDir } from '@redwoodjs/api'2export const handler = async () => {3 const zip = await zipDir('./test')4 return {5 headers: {6 'Content-Disposition': 'attachment; filename="test.zip"',7 },8 }9}10import { zipDir } from '@redwoodjs/api'11export const handler = async () => {12 const zip = await zipDir('./test', { name: 'test' })13 return {14 headers: {15 'Content-Disposition': 'attachment; filename="test.zip"',16 },17 }18}19import { zipDir } from '@redwoodjs/api'20export const handler = async () => {21 const zip = await zipDir('./test', { name: 'test', exclude: ['test'] })22 return {23 headers: {24 'Content-Disposition': 'attachment; filename="test.zip"',25 },26 }27}28import { zipDir } from '@redwoodjs/api'29export const handler = async () => {30 const zip = await zipDir('./test', {31 })32 return {33 headers: {34 'Content-Disposition': 'attachment; filename="test.zip"',35 },36 }37}38import { zipDir } from '@redwoodjs/api'39export const handler = async () => {40 const zip = await zipDir('./test', {41 })

Full Screen

Using AI Code Generation

copy

Full Screen

1import { zipDir } from '@redwoodjs/api'2export const handler = async (event, context) => {3 const zip = await zipDir('src')4 return {5 headers: {6 'Content-Disposition': 'attachment; filename="functions.zip"',7 },8 }9}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { zipDir } from '@redwoodjs/api'2export const handler = async (event, context) => {3 const { name } = event.queryStringParameters4 const zip = await zipDir({5 dir: path.join('/tmp', name),6 })7 return {8 headers: {9 },10 }11}12import archiver from 'archiver'13import { createWriteStream } from 'fs'14import { join } from 'path'15const output = createWriteStream(join('/tmp', 'test.zip'))16const archive = archiver('zip', {17 zlib: { level: 9 },18})19output.on('close', () => {20 console.log('Done')21})22archive.on('error', (err) => {23})24archive.pipe(output)25archive.directory(join('/tmp', 'test'), false)26archive.finalize()

Full Screen

Using AI Code Generation

copy

Full Screen

1import { zipDir } from '@redwoodjs/api'2export const handler = async () => {3 const zip = await zipDir('/path/to/dir', 'zipName')4 return {5 headers: {6 'Content-Disposition': 'attachment; filename=zipName.zip',7 },8 }9}10- [zipDir](#zipdir)11 - [Parameters](#parameters)12 - [Examples](#examples)13import { zipDir } from '@redwoodjs/api'14export const handler = async () => {15 const zip = await zipDir('/path/to/dir', 'zipName')16 return {17 headers: {18 'Content-Disposition': 'attachment; filename=zipName.zip',19 },20 }21}

Full Screen

Using AI Code Generation

copy

Full Screen

1const { zipDir } = require('@redwoodjs/api')2zipDir('src', 'src.zip').then(() => console.log('done!'))3const { unzip } = require('@redwoodjs/api')4unzip('src.zip', 'unzipped').then(() => console.log('done!'))5const { unzip } = require('@redwoodjs/api')6unzip('src.zip', 'unzipped').then(() => console.log('done!'))7const { unzip } = require('@redwoodjs/api')8unzip('src.zip', 'unzipped').then(() => console.log('done!'))9const { unzip } = require('@redwoodjs/api')10unzip('src.zip', 'unzipped').then(() => console.log('done!'))11const { unzip } = require('@redwoodjs/api')12unzip('src.zip', 'unzipped').then(() => console.log('done!'))13const { unzip } = require('@redwoodjs/api')14unzip('src.zip', 'unzipped').then(() => console.log('done!'))15const { unzip } = require('@redwoodjs/api')16unzip('src.zip', 'unzipped').then(() => console.log('done!'))17const { unzip } = require('@redwoodjs/api')18unzip('src.zip', 'unzipped').then(() => console.log('done!'))19const { unzip } = require('@redwoodjs/api')20unzip('src.zip', 'unzipped').then(() => console.log('done!'))21const { unzip } = require('@redwoodjs/api')22unzip('src.zip', 'unzipped').then(() => console.log('done!'))23const { unzip } = require('@redwoodjs/api')

Full Screen

Using AI Code Generation

copy

Full Screen

1import { zipDir } from '@redwoodjs/internal'2export const handler = async () => {3 zipDir('src', 'src.zip')4}5import { downloadFile } from '@redwoodjs/api'6export const handler = async () => {7 return downloadFile('src.zip')8}9import { unlinkSync } from 'fs'10export const handler = async () => {11 unlinkSync('src.zip')12}

Full Screen

Using AI Code Generation

copy

Full Screen

1var zipDir = require('redwoodfs').zipDir;2var zip = zipDir('/home/username/test', '/home/username/test.zip', function(err, zip) {3 if(err) {4 console.log(err);5 } else {6 console.log('zip created successfully');7 }8});9### zipDir(source, target, callback)10### unzip(zip, target, callback)

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful