dorky 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +21 -21
  3. package/index.js +294 -272
  4. package/package.json +1 -1
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Trishant Pahwa
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Trishant Pahwa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,22 +1,22 @@
1
- # dorky
2
- DevOps Records Keeper
3
-
4
- ## Steps to use:
5
-
6
- > Setup AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_REGION before hand.
7
-
8
- > Create a S3 bucket with the name `dorky` before start.
9
-
10
- ### To push files to S3 bucket.
11
- 1. Initialize dorky setup in the root folder of your project, using `dorky init`.
12
- 2. List the files using `dorky list`, (make sure to add excluded file or folder patterns to .dorkyignore, to minimize the list).
13
- 3. Add files to stage-1 using `dorky add file-name`.
14
- 4. Push files to S3 bucket using `dorky push`.
15
-
16
- ### To remove a file from project.
17
- 1. Remove files using `dorky reset file-name`.
18
- 2. Push files to S3 bucket using `dorky push`.
19
-
20
- ### To pull files from S3 bucket.
21
- 1. Initialize dorky project using `dorky init`.
1
+ # dorky
2
+ DevOps Records Keeper
3
+
4
+ ## Steps to use:
5
+
6
+ > Setup AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_REGION before hand.
7
+
8
+ > Create a S3 bucket with the name `dorky` before start.
9
+
10
+ ### To push files to S3 bucket.
11
+ 1. Initialize dorky setup in the root folder of your project, using `dorky init`.
12
+ 2. List the files using `dorky list`, (make sure to add excluded file or folder patterns to .dorkyignore, to minimize the list).
13
+ 3. Add files to stage-1 using `dorky add file-name`.
14
+ 4. Push files to S3 bucket using `dorky push`.
15
+
16
+ ### To remove a file from project.
17
+ 1. Remove files using `dorky reset file-name`.
18
+ 2. Push files to S3 bucket using `dorky push`.
19
+
20
+ ### To pull files from S3 bucket.
21
+ 1. Initialize dorky project using `dorky init`.
22
22
  2. Use `dorky pull` to pull the files from S3 bucket.
package/index.js CHANGED
@@ -1,272 +1,294 @@
1
- #!/usr/bin/env node
2
-
3
- const glob = require('glob');
4
- const path = require('path');
5
- const chalk = require('chalk');
6
- const fs = require('fs');
7
- const { EOL } = require('os');
8
- var AWS = require('aws-sdk');
9
- const { exit } = require('process');
10
-
11
-
12
- // Initializes project, creates a new .dorky folder, and adds a metadata file to it, and creates a .dorkyignore file.
13
- function initializeProject() {
14
- if (fs.existsSync('.dorky')) {
15
- console.log('Dorky project already initialized. Remove .dorky folder to reinitialize.')
16
- } else {
17
- fs.mkdirSync('.dorky');
18
- fs.writeFileSync('.dorky/metadata.json', JSON.stringify({ 'stage-1-files': [], 'uploaded-files': [] }));
19
- if (fs.existsSync('.dorkyignore')) {
20
- fs.rmdirSync('.dorky');
21
- console.log('Dorky project already initialized. Remove .dorkyignore file to reinitialize.');
22
- } else {
23
- fs.writeFileSync('.dorkyignore', '');
24
- console.log('Initialized project in current folder(.dorky).');
25
- }
26
- }
27
- }
28
-
29
- // Lists all the files that are not excluded explicitly.
30
- function listFiles() {
31
- let exclusions = fs.readFileSync('./.dorkyignore').toString().split(EOL);
32
- if (exclusions[0] == '') exclusions = [];
33
- var getDirectories = function (src, callback) {
34
- glob(src + '/**/*', callback);
35
- };
36
-
37
- function excludeIsPresent(element) {
38
- let present = false;
39
- let i = 0;
40
- while (i < exclusions.length) {
41
- if (element.includes(exclusions[i])) present = true;
42
- i += 1;
43
- }
44
- return present;
45
- }
46
- getDirectories(process.cwd(), function (err, res) {
47
- if (err) {
48
- console.log('Error', err);
49
- } else {
50
- let listOfFiles;
51
- listOfFiles = res.filter(element => !excludeIsPresent(element)).map(file => path.relative(process.cwd(), file));
52
- console.log(chalk.green('Found files:'))
53
- listOfFiles.map((file) => console.log('\t' + chalk.bgGrey(file)));
54
- }
55
- });
56
- }
57
-
58
- if (process.env.AWS_ACCESS_KEY && process.env.AWS_SECRET_KEY && process.env.AWS_REGION) {
59
- AWS.config.update({
60
- accessKeyId: process.env.AWS_ACCESS_KEY,
61
- secretAccessKey: process.env.AWS_SECRET_KEY,
62
- region: process.env.AWS_REGION
63
- });
64
- } else {
65
- console.log('Set AWS_ACCESS_KEY, AWS_SECRET_KEY and AWS_REGION first.')
66
- exit();
67
- }
68
-
69
- const args = process.argv.splice(2, 2);
70
-
71
- if (args.length == 0) {
72
- const helpMessage = `Help message:\ninit\t Initializes a dorky project.\nlist\t Lists files in current root directory.\npush\t Pushes changes to S3 bucket.\npull\t Pulls changes from S3 bucket to local root folder.`
73
- console.log(helpMessage);
74
- } else if (args.length == 1) {
75
- if (args[0] == 'init') {
76
- initializeProject();
77
- }
78
- if (args[0] == 'list') {
79
- listFiles();
80
- }
81
- if (args[0] == 'push') {
82
- console.log('Pushing files to server.');
83
- const rootFolder = process.cwd().split('\\').pop()
84
-
85
- function rootFolderExists(rootFolder) {
86
- let s3 = new AWS.S3();
87
- const bucketParams = { Bucket: 'dorky' };
88
- s3.listObjects(bucketParams, (err, s3Objects) => {
89
- if (err) console.log(err);
90
- else {
91
- if (s3Objects.Contents.filter((object) => object.Key.split('/')[0] == rootFolder).length > 0) {
92
- let metaData = JSON.parse(fs.readFileSync(path.join('.dorky', 'metadata.json')).toString());
93
- // Get removed files
94
- let removed = metaData['uploaded-files'].filter(x => !metaData['stage-1-files'].includes(x));
95
- // Uploaded added files.
96
- let added = metaData['stage-1-files'].filter(x => !metaData['uploaded-files'].includes(x));
97
-
98
- added.map((file) => {
99
- if (metaData['uploaded-files'].includes(file)) return;
100
- else {
101
- const putObjectParams = {
102
- Bucket: 'dorky',
103
- Key: path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'),
104
- Body: fs.readFileSync(path.relative(process.cwd(), file)).toString()
105
- }
106
- // Upload records
107
- s3.putObject(putObjectParams, (err, data) => {
108
- if (err) {
109
- console.log('Unable to upload file ' + path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'))
110
- console.log(err);
111
- }
112
- else console.log(chalk.green('Uploaded ' + file));
113
- });
114
- metaData['uploaded-files'].push(file);
115
- }
116
- });
117
-
118
- if (removed.length) {
119
- const removedObjectParams = {
120
- Bucket: 'dorky',
121
- Delete: {
122
- Objects: removed.map((file) => {
123
- return { Key: file };
124
- }),
125
- Quiet: true
126
- }
127
- }
128
- // Delete removed records, doesn't delete immediately.
129
- s3.deleteObjects(removedObjectParams, (err, data) => {
130
- if (err) console.log(err.stack);
131
- else console.log('Deleted removed files.');
132
- });
133
- }
134
- if (metaData['uploaded-files'] != metaData['stage-1-files']) {
135
- metaData['uploaded-files'] = Array.from(new Set(metaData['stage-1-files']));
136
- fs.writeFileSync(path.join('.dorky', 'metadata.json'), JSON.stringify(metaData));
137
- putObjectParams = {
138
- Bucket: 'dorky',
139
- Key: path.relative(process.cwd(), path.join(rootFolder.toString(), 'metadata.json')).replace(/\\/g, '/'),
140
- Body: JSON.stringify(metaData)
141
- }
142
- // Upload metadata.json
143
- s3.putObject(putObjectParams, (err, data) => {
144
- if (err) console.log(err);
145
- else console.log(chalk.green('Uploaded metadata'));
146
- });
147
- } else {
148
- console.log('Nothing to push');
149
- }
150
-
151
- } else {
152
-
153
- let metaData = JSON.parse(fs.readFileSync(path.join('.dorky', 'metadata.json')).toString());
154
- metaData['stage-1-files'].map((file) => {
155
- if (metaData['uploaded-files'].includes(file)) return;
156
- else {
157
- const putObjectParams = {
158
- Bucket: 'dorky',
159
- Key: path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'),
160
- Body: fs.readFileSync(path.relative(process.cwd(), file)).toString()
161
- }
162
- // Upload records
163
- s3.putObject(putObjectParams, (err, data) => {
164
- if (err) {
165
- console.log('Unable to upload file ' + path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'))
166
- console.log(err);
167
- }
168
- else console.log(chalk.green('Uploaded ' + file));
169
- });
170
- metaData['uploaded-files'].push(file);
171
- }
172
- });
173
- metaData['uploaded-files'] = Array.from(new Set(metaData['uploaded-files']));
174
- fs.writeFileSync(path.join('.dorky', 'metadata.json'), JSON.stringify(metaData));
175
- putObjectParams = {
176
- Bucket: 'dorky',
177
- Key: path.relative(process.cwd(), path.join(rootFolder.toString(), 'metadata.json')).replace(/\\/g, '/'),
178
- Body: JSON.stringify(metaData)
179
- }
180
- // Upload metadata.json
181
- s3.putObject(putObjectParams, (err, data) => {
182
- if (err) console.log(err);
183
- else console.log(chalk.green('Uploaded metadata'));
184
- });
185
- }
186
- }
187
- })
188
-
189
- }
190
- rootFolderExists(rootFolder);
191
- }
192
- if (args[0] == 'help') {
193
- const helpMessage = `Help message:\ninit\t Initializes a dorky project.\nlist\t Lists files in current root directory.\npush\t Pushes changes to S3 bucket.\npull\t Pulls changes from S3 bucket to local root folder.`
194
- console.log(helpMessage);
195
- }
196
- if (args[0] == 'pull') {
197
- console.log('Pulling files from server.')
198
- const rootFolder = process.cwd().split('\\').pop()
199
- let s3 = new AWS.S3();
200
- const bucketParams = { Bucket: 'dorky' };
201
- s3.listObjects(bucketParams, (err, s3Objects) => {
202
- if (err) console.log(err);
203
- else {
204
- if (s3Objects.Contents.filter((object) => object.Key.split('/')[0] == rootFolder).length > 0) {
205
- if (s3Objects.Contents.filter((object) => object.Key == (rootFolder + '/metadata.json')).length > 0) {
206
- const params = {
207
- Bucket: 'dorky',
208
- Key: rootFolder + '/metadata.json'
209
- }
210
- s3.getObject(params, (err, data) => {
211
- if (err) console.error(err);
212
- else {
213
- let metaData = JSON.parse(data.Body.toString());
214
- // Pull metadata.json
215
- const METADATA_FILE = '.dorky/metadata.json';
216
- fs.writeFileSync(METADATA_FILE, JSON.stringify(metaData));
217
- let pullFileParams;
218
- metaData['uploaded-files'].map((file) => {
219
- pullFileParams = {
220
- Bucket: 'dorky',
221
- Key: rootFolder + '/' + file
222
- }
223
- s3.getObject(pullFileParams, (err, data) => {
224
- if (err) console.log(err);
225
- else {
226
- console.log('Creating file ' + file);
227
- let fileData = data.Body.toString();
228
- let subDirectories = path.relative(process.cwd(), file).split('\\');
229
- subDirectories.pop()
230
- subDirectories = subDirectories.join('\\')
231
- if(subDirectories.length) fs.mkdirSync(subDirectories, { recursive: true });
232
- fs.writeFileSync(path.relative(process.cwd(), file), fileData);
233
- }
234
- })
235
- });
236
- }
237
- });
238
- } else {
239
- console.log('Metadata doesn\'t exist')
240
- }
241
- } else {
242
- console.error(chalk.red('Failed to pull folder, as it doesn\'t exist'));
243
- }
244
- }
245
- });
246
- }
247
- } else if (args.length == 2) {
248
- if (args[0] == 'add') {
249
- const METADATA_FILE = '.dorky/metadata.json';
250
- const file = args[1];
251
- if (fs.existsSync(file)) {
252
- const metaData = JSON.parse(fs.readFileSync(METADATA_FILE));
253
- const stage1Files = new Set(metaData['stage-1-files']);
254
- stage1Files.add(file);
255
- metaData['stage-1-files'] = Array.from(stage1Files);
256
- fs.writeFileSync(METADATA_FILE, JSON.stringify(metaData));
257
- console.log(chalk.bgGreen('Success'));
258
- console.log(chalk.green(`Added file ${file} successfully to stage-1.`))
259
- } else {
260
- console.log(chalk
261
- .bgRed('Error'))
262
- console.log(chalk.red(`\tFile ${file} doesn\'t exist`))
263
- }
264
- } else if (args[0] == 'reset') {
265
- const METADATA_FILE = '.dorky/metadata.json';
266
- const metaData = JSON.parse(fs.readFileSync(METADATA_FILE));
267
- const file = args[1];
268
- resetFileIndex = metaData['stage-1-files'].indexOf(file);
269
- metaData['stage-1-files'].splice(resetFileIndex, 1);
270
- fs.writeFileSync(METADATA_FILE, JSON.stringify(metaData));
271
- }
272
- }
1
+ #!/usr/bin/env node
2
+
3
+ const glob = require('glob');
4
+ const path = require('path');
5
+ const chalk = require('chalk');
6
+ const fs = require('fs');
7
+ const { EOL } = require('os');
8
+ var AWS = require('aws-sdk');
9
+ const { exit } = require('process');
10
+
11
+
12
+ // Initializes project, creates a new .dorky folder, and adds a metadata file to it, and creates a .dorkyignore file.
13
+ function initializeProject() {
14
+ if (fs.existsSync('.dorky')) {
15
+ console.log('Dorky project already initialized. Remove .dorky folder to reinitialize.')
16
+ } else {
17
+ fs.mkdirSync('.dorky');
18
+ fs.writeFileSync('.dorky/metadata.json', JSON.stringify({ 'stage-1-files': [], 'uploaded-files': [] }));
19
+ if (fs.existsSync('.dorkyignore')) {
20
+ fs.rmdirSync('.dorky');
21
+ console.log('Dorky project already initialized. Remove .dorkyignore file to reinitialize.');
22
+ } else {
23
+ fs.writeFileSync('.dorkyignore', '');
24
+ console.log('Initialized project in current folder(.dorky).');
25
+ }
26
+ }
27
+ }
28
+
29
+ // Lists all the files that are not excluded explicitly.
30
+ function listFiles() {
31
+ let exclusions = fs.readFileSync('./.dorkyignore').toString().split(EOL);
32
+ exclusions = exclusions.filter((exclusion) => exclusion !== '');
33
+ if (exclusions[0] == '') exclusions = [];
34
+ var getDirectories = function (src, callback) {
35
+ glob(src + '/**/*', callback);
36
+ };
37
+
38
+ function excludeIsPresent(element) {
39
+ let present = false;
40
+ let i = 0;
41
+ while (i < exclusions.length) {
42
+ if (element.includes(exclusions[i])) present = true;
43
+ i += 1;
44
+ }
45
+ return present;
46
+ }
47
+ getDirectories(process.cwd(), function (err, res) {
48
+ if (err) {
49
+ console.log('Error', err);
50
+ } else {
51
+ let listOfFiles;
52
+ listOfFiles = res.filter(element => !excludeIsPresent(element)).map(file => path.relative(process.cwd(), file));
53
+ console.log(chalk.green('Found files:'))
54
+ listOfFiles.map((file) => console.log('\t' + chalk.bgGrey(file)));
55
+ }
56
+ });
57
+ }
58
+
59
+ if (process.env.AWS_ACCESS_KEY && process.env.AWS_SECRET_KEY && process.env.AWS_REGION) {
60
+ AWS.config.update({
61
+ accessKeyId: process.env.AWS_ACCESS_KEY,
62
+ secretAccessKey: process.env.AWS_SECRET_KEY,
63
+ region: process.env.AWS_REGION
64
+ });
65
+ } else {
66
+ console.log('Set AWS_ACCESS_KEY, AWS_SECRET_KEY and AWS_REGION first.')
67
+ exit();
68
+ }
69
+
70
+ const args = process.argv.splice(2, 2);
71
+
72
+ if (args.length == 0) {
73
+ const helpMessage = `Help message:\ninit\t Initializes a dorky project.\nlist\t Lists files in current root directory.\npush\t Pushes changes to S3 bucket.\npull\t Pulls changes from S3 bucket to local root folder.`
74
+ console.log(helpMessage);
75
+ } else if (args.length == 1) {
76
+ if (args[0] == 'init') {
77
+ initializeProject();
78
+ }
79
+ if (args[0] == 'list') {
80
+ listFiles();
81
+ }
82
+ if (args[0] == 'push') {
83
+ console.log('Pushing files to server.');
84
+ let rootFolder;
85
+ if (process.cwd().includes('\\')) {
86
+ rootFolder = process.cwd().split('\\').pop()
87
+ } else if (process.cwd().includes('/')) {
88
+ rootFolder = process.cwd().split('/').pop()
89
+ } else rootFolder = process.cwd()
90
+ console.log(rootFolder)
91
+ function rootFolderExists(rootFolder) {
92
+ let s3 = new AWS.S3();
93
+ const bucketParams = { Bucket: 'dorky' };
94
+ s3.listObjects(bucketParams, (err, s3Objects) => {
95
+ if (err) console.log(err);
96
+ else {
97
+ if (s3Objects.Contents.filter((object) => object.Key.split('/')[0] == rootFolder).length > 0) {
98
+ let metaData = JSON.parse(fs.readFileSync(path.join('.dorky', 'metadata.json')).toString());
99
+ // Get removed files
100
+ let removed = metaData['uploaded-files'].filter(x => !metaData['stage-1-files'].includes(x));
101
+ // Uploaded added files.
102
+ let added = metaData['stage-1-files'].filter(x => !metaData['uploaded-files'].includes(x));
103
+
104
+ added.map((file) => {
105
+ if (metaData['uploaded-files'].includes(file)) return;
106
+ else {
107
+ const putObjectParams = {
108
+ Bucket: 'dorky',
109
+ Key: path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'),
110
+ Body: fs.readFileSync(path.relative(process.cwd(), file)).toString()
111
+ }
112
+ // Upload records
113
+ s3.putObject(putObjectParams, (err, data) => {
114
+ if (err) {
115
+ console.log('Unable to upload file ' + path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'))
116
+ console.log(err);
117
+ }
118
+ else console.log(chalk.green('Uploaded ' + file));
119
+ });
120
+ metaData['uploaded-files'].push(file);
121
+ }
122
+ });
123
+
124
+ if (removed.length) {
125
+ const removedObjectParams = {
126
+ Bucket: 'dorky',
127
+ Delete: {
128
+ Objects: removed.map((file) => {
129
+ return { Key: file };
130
+ }),
131
+ Quiet: true
132
+ }
133
+ }
134
+ // Delete removed records, doesn't delete immediately.
135
+ s3.deleteObjects(removedObjectParams, (err, data) => {
136
+ if (err) console.log(err.stack);
137
+ else console.log('Deleted removed files.');
138
+ });
139
+ }
140
+ if (metaData['uploaded-files'] != metaData['stage-1-files']) {
141
+ metaData['uploaded-files'] = Array.from(new Set(metaData['stage-1-files']));
142
+ fs.writeFileSync(path.join('.dorky', 'metadata.json'), JSON.stringify(metaData));
143
+ putObjectParams = {
144
+ Bucket: 'dorky',
145
+ Key: path.relative(process.cwd(), path.join(rootFolder.toString(), 'metadata.json')).replace(/\\/g, '/'),
146
+ Body: JSON.stringify(metaData)
147
+ }
148
+ // Upload metadata.json
149
+ s3.putObject(putObjectParams, (err, data) => {
150
+ if (err) console.log(err);
151
+ else console.log(chalk.green('Uploaded metadata'));
152
+ });
153
+ } else {
154
+ console.log('Nothing to push');
155
+ }
156
+
157
+ } else {
158
+
159
+ let metaData = JSON.parse(fs.readFileSync(path.join('.dorky', 'metadata.json')).toString());
160
+ metaData['stage-1-files'].map((file) => {
161
+ if (metaData['uploaded-files'].includes(file)) return;
162
+ else {
163
+ const putObjectParams = {
164
+ Bucket: 'dorky',
165
+ Key: path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'),
166
+ Body: fs.readFileSync(path.relative(process.cwd(), file)).toString()
167
+ }
168
+ // Upload records
169
+ s3.putObject(putObjectParams, (err, data) => {
170
+ if (err) {
171
+ console.log('Unable to upload file ' + path.join(rootFolder, path.relative(process.cwd(), file)).replace(/\\/g, '/'))
172
+ console.log(err);
173
+ }
174
+ else console.log(chalk.green('Uploaded ' + file));
175
+ });
176
+ metaData['uploaded-files'].push(file);
177
+ }
178
+ });
179
+ metaData['uploaded-files'] = Array.from(new Set(metaData['uploaded-files']));
180
+ fs.writeFileSync(path.join('.dorky', 'metadata.json'), JSON.stringify(metaData));
181
+ putObjectParams = {
182
+ Bucket: 'dorky',
183
+ Key: path.relative(process.cwd(), path.join(rootFolder.toString(), 'metadata.json')).replace(/\\/g, '/'),
184
+ Body: JSON.stringify(metaData)
185
+ }
186
+ // Upload metadata.json
187
+ s3.putObject(putObjectParams, (err, data) => {
188
+ if (err) console.log(err);
189
+ else console.log(chalk.green('Uploaded metadata'));
190
+ });
191
+ }
192
+ }
193
+ })
194
+
195
+ }
196
+ rootFolderExists(rootFolder);
197
+ }
198
+ if (args[0] == 'help') {
199
+ const helpMessage = `Help message:\ninit\t Initializes a dorky project.\nlist\t Lists files in current root directory.\npush\t Pushes changes to S3 bucket.\npull\t Pulls changes from S3 bucket to local root folder.`
200
+ console.log(helpMessage);
201
+ }
202
+ if (args[0] == 'pull') {
203
+ console.log('Pulling files from server.')
204
+ let rootFolder;
205
+ if (process.cwd().includes('\\')) {
206
+ rootFolder = process.cwd().split('\\').pop()
207
+ } else if (process.cwd().includes('/')) {
208
+ rootFolder = process.cwd().split('/').pop()
209
+ } else rootFolder = process.cwd()
210
+ let s3 = new AWS.S3();
211
+ const bucketParams = { Bucket: 'dorky' };
212
+ s3.listObjects(bucketParams, (err, s3Objects) => {
213
+ if (err) console.log(err);
214
+ else {
215
+ if (s3Objects.Contents.filter((object) => object.Key.split('/')[0] == rootFolder).length > 0) {
216
+ if (s3Objects.Contents.filter((object) => object.Key == (rootFolder + '/metadata.json')).length > 0) {
217
+ const params = {
218
+ Bucket: 'dorky',
219
+ Key: rootFolder + '/metadata.json'
220
+ }
221
+ s3.getObject(params, (err, data) => {
222
+ if (err) console.error(err);
223
+ else {
224
+ let metaData = JSON.parse(data.Body.toString());
225
+ // Pull metadata.json
226
+ const METADATA_FILE = '.dorky/metadata.json';
227
+ fs.writeFileSync(METADATA_FILE, JSON.stringify(metaData));
228
+ let pullFileParams;
229
+ metaData['uploaded-files'].map((file) => {
230
+ pullFileParams = {
231
+ Bucket: 'dorky',
232
+ Key: rootFolder + '/' + file
233
+ }
234
+ s3.getObject(pullFileParams, (err, data) => {
235
+ if (err) console.log(err);
236
+ else {
237
+ console.log('Creating file ' + file);
238
+ let fileData = data.Body.toString();
239
+ let subDirectories;
240
+ if (process.cwd().includes('\\')) {
241
+ subDirectories = path.relative(process.cwd(), file).split('\\');
242
+ } else if (process.cwd().includes('/')) {
243
+ subDirectories = path.relative(process.cwd(), file).split('/');
244
+ } else subDirectories = path.relative(process.cwd(), file)
245
+ console.log(subDirectories)
246
+ subDirectories.pop()
247
+ console.log(subDirectories)
248
+ if (process.platform === "win32") {
249
+ subDirectories = subDirectories.join('\\')
250
+ } else if (process.platform === "linux" || process.platform === "darwin") {
251
+ subDirectories = subDirectories.join('/');
252
+ }
253
+ if (subDirectories.length) fs.mkdirSync(subDirectories, { recursive: true });
254
+ fs.writeFileSync(path.relative(process.cwd(), file), fileData);
255
+ }
256
+ })
257
+ });
258
+ }
259
+ });
260
+ } else {
261
+ console.log('Metadata doesn\'t exist')
262
+ }
263
+ } else {
264
+ console.error(chalk.red('Failed to pull folder, as it doesn\'t exist'));
265
+ }
266
+ }
267
+ });
268
+ }
269
+ } else if (args.length == 2) {
270
+ if (args[0] == 'add') {
271
+ const METADATA_FILE = '.dorky/metadata.json';
272
+ const file = args[1];
273
+ if (fs.existsSync(file)) {
274
+ const metaData = JSON.parse(fs.readFileSync(METADATA_FILE));
275
+ const stage1Files = new Set(metaData['stage-1-files']);
276
+ stage1Files.add(file);
277
+ metaData['stage-1-files'] = Array.from(stage1Files);
278
+ fs.writeFileSync(METADATA_FILE, JSON.stringify(metaData));
279
+ console.log(chalk.bgGreen('Success'));
280
+ console.log(chalk.green(`Added file ${file} successfully to stage-1.`))
281
+ } else {
282
+ console.log(chalk
283
+ .bgRed('Error'))
284
+ console.log(chalk.red(`\tFile ${file} doesn\'t exist`))
285
+ }
286
+ } else if (args[0] == 'reset') {
287
+ const METADATA_FILE = '.dorky/metadata.json';
288
+ const metaData = JSON.parse(fs.readFileSync(METADATA_FILE));
289
+ const file = args[1];
290
+ resetFileIndex = metaData['stage-1-files'].indexOf(file);
291
+ metaData['stage-1-files'].splice(resetFileIndex, 1);
292
+ fs.writeFileSync(METADATA_FILE, JSON.stringify(metaData));
293
+ }
294
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dorky",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "DevOps Records Keeper.",
5
5
  "main": "index.js",
6
6
  "bin": {