-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from bhoudu/develop
Develop
- Loading branch information
Showing
7 changed files
with
162 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ lerna-debug.log* | |
/reports | ||
/.nyc_output | ||
test-report.html | ||
test-folder | ||
|
||
# IDEs and editors | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,21 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from 'fs'; | ||
import archiver from 'archiver'; | ||
|
||
interface ZambdaFile { | ||
source: string; | ||
destination: string; | ||
} | ||
|
||
type ZambdaFolder = string | ZambdaFile; | ||
|
||
interface ZambdaZip { | ||
name: string; | ||
folders: ZambdaFolder[]; | ||
files: ZambdaFile[]; | ||
} | ||
|
||
interface ZambdaConfig { | ||
workDir: string; | ||
zip: ZambdaZip; | ||
} | ||
|
||
async function zipLambda( | ||
configFilePath: string, | ||
): Promise<boolean> { | ||
const configJson: string = fs.readFileSync(configFilePath, { | ||
encoding: 'UTF-8', | ||
}); | ||
if (!configJson) { | ||
throw new Error('JSON file: ' + configFilePath + ' cannot be read!'); | ||
} | ||
|
||
const zambdaConfig = JSON.parse(configJson) as ZambdaConfig; | ||
const workPath = zambdaConfig.workDir.endsWith('/') ? zambdaConfig.workDir : zambdaConfig.workDir + '/'; | ||
return new Promise((resolve, reject) => { | ||
const destinationPath = workPath + zambdaConfig.zip.name; | ||
|
||
// Init archive | ||
const output = fs.createWriteStream(destinationPath); | ||
const archive = archiver('zip', { | ||
zlib: { level: 9 }, // Sets the compression level. | ||
}); | ||
|
||
// Listeners on zip | ||
output.on('close', function close(): void { | ||
console.log(archive.pointer() + ' total bytes'); | ||
resolve(true); | ||
}); | ||
output.on('end', reject); | ||
|
||
// Listeners on archive | ||
archive.on('warning', function warn(err): void { | ||
if (err.code === 'ENOENT') { | ||
// log warning | ||
console.warn('Error found: ' + err); | ||
return; | ||
} | ||
reject(err); | ||
}); | ||
archive.on('error', reject); | ||
archive.pipe(output); | ||
|
||
// Append files | ||
zambdaConfig.zip?.files.forEach(f => { | ||
const filePath = f.source; | ||
const fileName = !!f.destination ? f.destination : f.source; | ||
archive.file(filePath, { name: fileName }); | ||
}); | ||
zambdaConfig.zip?.folders.forEach(f => { | ||
const isString = typeof f === 'string' || f instanceof String; | ||
if (isString) { | ||
const folder = f as string; | ||
const directory = folder.endsWith('/') ? f : f + '/'; | ||
const destination = folder.endsWith('/') ? folder.substring(0, folder.length - 1) : folder; | ||
archive.directory(directory, destination); | ||
} else { | ||
const zambdaFile = f as ZambdaFile; | ||
const source = zambdaFile.source.endsWith('/') ? zambdaFile.source | ||
: zambdaFile.source + '/'; | ||
const destination = zambdaFile.destination.endsWith('/') ? | ||
zambdaFile.destination.substring(0, zambdaFile.destination.length - 1) | ||
: zambdaFile.destination; | ||
archive.directory(source, destination); | ||
} | ||
}); | ||
|
||
// Do the archive! | ||
archive.finalize(); | ||
}); | ||
} | ||
import { zip } from "./zip"; | ||
|
||
const isOK = process.argv.length > 2; | ||
if (!isOK) { | ||
console.error('You must provide a zambda conf file!'); | ||
process.exit(1); | ||
} | ||
|
||
// Generate package | ||
// Zip! | ||
const confFile = process.argv[2]; | ||
zipLambda(confFile).then(function handler(): void { | ||
console.log('Zambda archive for conf: ' + confFile + ' has been generated!'); | ||
}).catch(e => { | ||
console.error(e + ''); | ||
process.exit(1); | ||
}); | ||
zip(confFile) | ||
.then(function handler(): void { | ||
console.log('Zambda archive for conf: ' + confFile + ' has been generated!'); | ||
process.exit(0); | ||
}) | ||
.catch(e => { | ||
console.error(e + ''); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { zipWithConf } from "./zip"; | ||
|
||
describe('zip.ts', () => { | ||
it('should zip file from given json conf', async () => { | ||
const result = await zipWithConf({ | ||
workDir: 'test-folder', | ||
zip: { | ||
name: 'test.zip', | ||
files: [ | ||
{ | ||
source: 'README.md' | ||
}, | ||
{ | ||
source: 'package.json', | ||
destination: 'subpath/package.json', | ||
} | ||
], | ||
folders: [ | ||
'resources', | ||
{ | ||
source: 'src', | ||
destination: 'subpath/src', | ||
} | ||
], | ||
} | ||
}); | ||
expect(result).toBeTruthy(); | ||
}, 10000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import fs from "fs"; | ||
import archiver from 'archiver'; | ||
import * as mkdirp from "mkdirp"; | ||
|
||
interface ZambdaFile { | ||
source: string; | ||
destination?: string; | ||
} | ||
|
||
type ZambdaFolder = string | ZambdaFile; | ||
|
||
interface ZambdaZip { | ||
name: string; | ||
folders: ZambdaFolder[]; | ||
files: ZambdaFile[]; | ||
} | ||
|
||
interface ZambdaConfig { | ||
workDir: string; | ||
zip: ZambdaZip; | ||
} | ||
|
||
export function zipWithConf( | ||
zambdaConfig: ZambdaConfig, | ||
): Promise<boolean> { | ||
// Define workPath | ||
const workPath = zambdaConfig.workDir.endsWith('/') ? zambdaConfig.workDir : zambdaConfig.workDir + '/'; | ||
return new Promise((resolve, reject) => { | ||
// Create work dir | ||
mkdirp.sync(workPath); | ||
|
||
// Define target zip file | ||
const destinationPath = workPath + zambdaConfig.zip.name; | ||
|
||
// Init archive | ||
const output = fs.createWriteStream(destinationPath); | ||
const archive = archiver('zip', { | ||
zlib: { level: 9 }, // Sets the compression level. | ||
}); | ||
|
||
// Listeners on zip | ||
output.on('close', function close(): void { | ||
console.log(archive.pointer() + ' total bytes'); | ||
resolve(true); | ||
}); | ||
output.on('end', reject); | ||
|
||
// Listeners on archive | ||
archive.on('warning', function warn(err): void { | ||
if (err.code === 'ENOENT') { | ||
// log warning | ||
console.warn('Error found: ' + err); | ||
return; | ||
} | ||
reject(err); | ||
}); | ||
archive.on('error', reject); | ||
|
||
// Append files | ||
zambdaConfig.zip?.files.forEach(f => { | ||
const filePath = f.source; | ||
const fileName = !!f.destination ? f.destination : f.source; | ||
archive.file(filePath, { name: fileName }); | ||
}); | ||
|
||
// Append folders | ||
zambdaConfig.zip?.folders.forEach(f => { | ||
const isString = typeof f === 'string' || f instanceof String; | ||
if (isString) { | ||
const folder = f as string; | ||
const directory = folder.endsWith('/') ? f : f + '/'; | ||
const destination = folder.endsWith('/') ? folder.substring(0, folder.length - 1) : folder; | ||
archive.directory(directory, destination); | ||
} else { | ||
const zambdaFile = f as ZambdaFile; | ||
const source = zambdaFile.source.endsWith('/') ? zambdaFile.source | ||
: zambdaFile.source + '/'; | ||
const destination = zambdaFile.destination.endsWith('/') ? | ||
zambdaFile.destination.substring(0, zambdaFile.destination.length - 1) | ||
: zambdaFile.destination; | ||
archive.directory(source, destination); | ||
} | ||
}); | ||
|
||
// Pipe all the files | ||
archive.pipe(output); | ||
|
||
// Do the archive! | ||
archive.finalize(); | ||
}); | ||
} | ||
|
||
export async function zip( | ||
configFilePath: string, | ||
): Promise<boolean> { | ||
const configJson: string = fs.readFileSync(configFilePath, { | ||
encoding: 'UTF-8', | ||
}); | ||
if (!configJson) { | ||
throw new Error('JSON file: ' + configFilePath + ' cannot be read!'); | ||
} | ||
// Parse configuration | ||
const zambdaConfig = JSON.parse(configJson) as ZambdaConfig; | ||
return zipWithConf(zambdaConfig).then(() => { | ||
console.log('gfg'); | ||
return true; | ||
}); | ||
} |