Skip to content

Commit 89e3132

Browse files
author
Sébastien Van Eyck
committed
feat(assets|INS-500): NPM library must parse an assets folder and sync it with Frontify
1 parent 95a9ba8 commit 89e3132

File tree

6 files changed

+79
-28
lines changed

6 files changed

+79
-28
lines changed

assets/level_1/top-app-menu.png

151 KB
Loading

index.js

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

33
var main = require('./src/main');
4-
main.init('./patterns');
4+
main.init();

src/files.service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function readDir(path) {
1616
return fs.readdirSync(path);
1717
}
1818
catch(err) {
19-
var error = "Error:" + err.message;
19+
var error = err.message;
2020
Logger.error(error);
2121
return null;
2222
}
@@ -37,7 +37,7 @@ function checkIfDir(path, silent) {
3737
if (silent) {
3838
return false;
3939
}
40-
var error = "Error: " + err.message;
40+
var error = err.message;
4141
Logger.error(error);
4242
return false;
4343
}

src/frontify.service.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
var _ = require('lodash');
4+
5+
var frontifyApi = require('@frontify/frontify-api');
6+
var frontifyPattern = require('@frontify/frontify-api/lib/core/patterns.js');
7+
var Logger = require('./logger.service');
8+
9+
/**
10+
* Synchronize remote Frontify patterns
11+
* @param {Object} access Frontify API credentials
12+
* @param {Array} files Array of JSON files to sync
13+
*/
14+
function syncPatterns(access, files) {
15+
frontifyApi.syncPatterns(access, files)
16+
.then(function(data) {
17+
Logger.success('Patterns have been synchronized to Frontify');
18+
})
19+
.catch(function(err) {
20+
Logger.error(err);
21+
});
22+
}
23+
24+
/**
25+
* Synchronize remote Frontify assets
26+
* @param {Object} access Frontify API credentials
27+
* @param {Array} files Array of files to sync
28+
* @param {String} target Destination directory in Frontify
29+
* @param {String} cwd Current local working directory
30+
*/
31+
function syncAssets(access, files, target, cwd) {
32+
if (_.isString(target)) {
33+
assets.target = target;
34+
}
35+
if (_.isString(cwd)) {
36+
assets.cwd = cwd;
37+
}
38+
39+
frontifyApi.syncAssets(access, files)
40+
.then(function(data) {
41+
Logger.success('Assets have been synchronized to Frontify');
42+
})
43+
.catch(function(err) {
44+
Logger.error(err);
45+
});
46+
}
47+
48+
module.exports = {
49+
syncPatterns: syncPatterns,
50+
syncAssets: syncAssets
51+
};

src/logger.service.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ function log(type, message) {
4141
color = "green";
4242
break;
4343
default:
44+
type = '';
4445
color = "white";
4546
}
46-
console.log(colors[color](message));
47+
type += ':';
48+
console.log(colors[color](type.toUpperCase()), message);
4749
}
4850

4951
module.exports = {

src/main.js

+22-24
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@
33
var fs = require('fs');
44
var _ = require('lodash');
55

6-
var frontifyApi = require('@frontify/frontify-api');
7-
var frontifyPattern = require('@frontify/frontify-api/lib/core/patterns.js');
8-
96
var Patterns = require('./patterns.service');
107
var Files = require('./files.service');
8+
var Frontify = require('./frontify.service');
119
var Logger = require('./logger.service');
1210

13-
function init(patternsFolder) {
11+
12+
var access = {
13+
access_token: "frontify_access_token",
14+
project: "frontify_project_id"
15+
};
16+
var patternsDir = './patterns';
17+
var patternsJSONFiles = [
18+
'tmp-frontify/**/*.json'
19+
];
20+
var assetsFiles = [
21+
'assets/**/*.*'
22+
];
23+
24+
/**
25+
* Generate patterns JSON files, then sync them and sync assets
26+
*/
27+
function init() {
1428
try {
15-
generatePatterns(patternsFolder);
29+
generatePatterns(patternsDir);
1630
}
1731
catch(err) {
1832
Logger.error(err);
1933
return;
2034
}
21-
synchronizePatterns();
35+
Frontify.syncPatterns(access, patternsJSONFiles);
36+
Frontify.syncAssets(access, assetsFiles);
2237
}
2338

24-
2539
/**
2640
* Generate JSON files for found patterns
41+
* @param {String} patternsFolder Root directory of patterns files
2742
*/
2843
function generatePatterns(patternsFolder) {
2944
walk(patternsFolder);
@@ -83,23 +98,6 @@ function walk(dir, currentPattern, startPattern) {
8398
});
8499
}
85100

86-
/**
87-
* Synchronize remote Frontify project
88-
*/
89-
function synchronizePatterns() {
90-
var access = {
91-
access_token: "frontify_access_token",
92-
project: "frontify_project_id"
93-
};
94-
frontifyApi.syncPatterns(access, ['tmp-frontify/**/*.json'])
95-
.then(function(data) {
96-
Logger.success('Success: Patterns have been synchronized to Frontify');
97-
})
98-
.catch(function(err) {
99-
Logger.error(err);
100-
});
101-
}
102-
103101
module.exports = {
104102
init: init
105103
};

0 commit comments

Comments
 (0)