import * as AdmZip from 'adm-zip'; import chalk from 'chalk'; import * as child_process from 'child_process'; import * as fs from 'fs'; import * as glob from 'glob'; import * as path from 'path'; export class AppPackager { private rootPath: string; constructor(rootPath: string) { this.rootPath = path.resolve(rootPath); } public package() { console.log(chalk.gray(`Packaging files in ${this.rootPath}`)); const files = glob.sync('**/*', { cwd: this.rootPath, dot: true, ignore: ['node_modules/**/*', '*.zip', '.git/**/*'], nodir: true }).filter(this.filterGitIgnored); return this.createZip(this.rootPath, files); } private createZip(srcPath: string, files: string[]): Buffer { const zip = new AdmZip(); files.forEach((file) => { const filePath = path.join(srcPath, file); if (fs.existsSync(filePath)) { console.log(chalk.gray(`${file}`)); zip.addFile(file, fs.readFileSync(filePath)); } }); return zip.toBuffer(); } private filterGitIgnored = (file: string) => { return child_process.spawnSync( 'git', ['check-ignore', '-q', file], {cwd: this.rootPath} ).status === 1; } }