Skip to content

Commit 2c9a115

Browse files
committed
add zip function
1 parent 7220a41 commit 2c9a115

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict';
22

3-
module.exports = require('./lib/mocha');
3+
module.exports = require('./lib/');

lib/index.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const { exec } = require("child_process");
22
const { post } = require("axios")
33
const { readFileSync } = require("fs");
44
const path = require('path')
5-
const prompt = require('prompt')
6-
const endpoint = 'https://nhfz4ymqhc.execute-api.ap-northeast-2.amazonaws.com/dev/auth'
5+
const prompt = require('prompt');
6+
const zip = require('./zip')
7+
const endpoint = 'https://2j12cf7y29.execute-api.ap-northeast-2.amazonaws.com/dev/'
78
const payload = {
89
"name": path.basename(process.cwd()),
910
"assessments": [{
@@ -38,11 +39,11 @@ const getEmail = () => {
3839
}
3940

4041
const getEndpoint = (email) => {
41-
post(endpoint, {
42+
post(endpoint + 'auth', {
4243
email
4344
})
4445
.then(res => {
45-
runReport(result => submit(result, res.data))
46+
runReport(async result => await submit(result, res.data))
4647
})
4748
.catch(err => {
4849
if (err.response.status === 404) {
@@ -64,17 +65,36 @@ const runReport = (callback) => {
6465
})
6566
}
6667

67-
const submit = (result, auth) => {
68+
const submit = async (result, auth) => {
6869
payload.user = auth.users[0]
6970
payload.assessments[0].result = result
71+
const filename = `${payload.user.email}--${new Date().toISOString().replace(/\:/g, '-')}.zip`
7072

71-
post(auth.endpoint_submit, payload)
73+
// console.log(payload)
74+
const config = {
75+
headers: {
76+
'Content-Type': 'application/json'
77+
}
78+
}
79+
80+
post(auth.endpoint_submit, payload, config)
7281
.then(resp => {
7382
console.log('제출에 성공하였습니다.')
7483
})
7584
.catch(err => {
85+
console.log(err)
7686
throw new Error('제출에 실패하였습니다.')
7787
})
88+
89+
const base64 = await zip(filename)
90+
post(auth.endpoint_upload, { contents: base64, filename }, config)
91+
.then(resp => {
92+
console.log('과제 업로드에 성공하였습니다.')
93+
})
94+
.catch(err => {
95+
console.log(err)
96+
throw new Error('과제 업로드에 실패하였습니다.')
97+
})
7898
}
7999

80100
module.exports = getEmail;

lib/zip.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { createWriteStream } = require("fs")
2+
const archiver = require("archiver")
3+
const { Base64Encode } = require('base64-stream')
4+
const getStream = require('get-stream')
5+
6+
const zip = (filename) => {
7+
const out = createWriteStream(filename)
8+
const archive = archiver('zip', { zlib: { level: 9 }})
9+
const encoder = new Base64Encode()
10+
11+
return new Promise((resolve, reject) => {
12+
archive
13+
.glob('**', {
14+
ignore: ['node_modules/**', filename]
15+
})
16+
.on('error', err => reject(err))
17+
.pipe(out)
18+
19+
let base64str;
20+
getStream(archive.pipe(encoder)).then(str => {
21+
base64str = str
22+
})
23+
24+
out.on('close', async () => {
25+
// console.log(base64str)
26+
resolve(base64str)
27+
})
28+
29+
archive.finalize()
30+
})
31+
}
32+
33+
module.exports = zip;

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@codestates/submission-npm",
3-
"version": "1.0.5",
2+
"name": "@codestates-cc/submission-npm",
3+
"version": "1.0.7",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -14,13 +14,16 @@
1414
},
1515
"repository": {
1616
"type": "git",
17-
"url": "git://github.com/codestates/codestates-submission-npm.git"
17+
"url": "git://github.com/codestates-cc/submission-npm.git"
1818
},
1919
"keywords": [],
2020
"author": "Hoyong Lee",
2121
"license": "ISC",
2222
"dependencies": {
23+
"archiver": "^5.1.0",
2324
"axios": "^0.21.0",
25+
"base64-stream": "^1.0.0",
26+
"get-stream": "^6.0.0",
2427
"prompt": "^1.0.0"
2528
}
2629
}

0 commit comments

Comments
 (0)