Skip to content

Commit c942548

Browse files
committed
Working on automating the import & update process for #1
1 parent dc63762 commit c942548

File tree

10 files changed

+3336
-492
lines changed

10 files changed

+3336
-492
lines changed

README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The project is a quick starter for AngularJS applications consuming content from Contentful, a content as a service provider.
44

5+
You just have to sign-up for a Contentful account which is totally free, create a space and grab your "Management" API key for when you initialize the project. We'll import the sample content model along with the content to your space so you have a starting point to experiment from. It's quick and simple to register for a space, as is the upload process for the sample data into your own space.
6+
7+
You can then customize the content model to your liking as the content model and data will be under your own personal Contentful space. Shout-out to [Stefan Judis](https://github.com/stefanjudis) from Contentful for the heads up about the import & export modules.
8+
59
<img src="https://images.contentful.com/fo9twyrwpveg/44baP9Gtm8qE2Umm8CQwQk/c43325463d1cb5db2ef97fca0788ea55/PoweredByContentful_LightBackground.svg" width="225" style="margin-left: 20px;" /><img src="https://angular.io/assets/images/logos/angular/angular.png" width="80" /><img src="https://v4-alpha.getbootstrap.com/assets/brand/bootstrap-solid.svg" width="60" />
610

711

@@ -27,22 +31,24 @@ and npm is simply a node package which is a package manage in itself. It allows
2731

2832
Just [fork](https://github.com/joshhebb/angularjs-contentful-starter) or pull down the repository and run the following commands.
2933

34+
**Before you start** when you run the final step of this setup, you will be asked to enter your space ID and management API key from Contentful. If you haven't registered for a account [head over to Contentful](https://www.contentful.com/sign-up/#dev) and get started.
35+
3036
```shell
3137
git clone https://github.com/joshhebb/angularjs-contentful-starter.git your-project-name-here
3238
cd your-project-name-here
33-
npm install -g gulp # Install Gulp (global)
34-
npm install -g bower # Install Bower (global)
35-
npm run init # Run init task to install the node modules, libraries and then call gulp to build the app
39+
npm install -g gulp # Install Gulp (global)
40+
npm install -g bower # Install Bower (global)
41+
npm run init # Run init task to install the node modules, libraries and then call gulp to build the app
3642
```
3743

38-
### Contentful Integration
44+
If everything goes okay, you will have imported the content model into your space and built the project. You're now ready to start development!
3945

40-
The project relies heavily on Contentful which is a headless CMS. It's a content as a service provider that exposes it's content through rest APIs exposing content as JSON, easily consumed in Angular.
4146

42-
When you pull down the project - it will be pointed at my Contentful space which has content to imitate an Apple store selling some categorized products, with some additional store information hosted on the site as well as a simple blog. I was curious how the component relationships would work and it is what drove my content modelling.
47+
### Contentful Integration
4348

44-
You can either experiment with the JSON that's returned to you, or rip out any implementation details in the context of my Contentful space and start on your own content model.
49+
The project relies heavily on Contentful which is a headless CMS. It's a content as a service provider that exposes it's content through rest APIs exposing content as JSON, easily consumed in Angular.
4550

51+
One of the cool things about Contentful is that it ships with an export and an import API - so the project is setup to upload the sample content model that I created, along with sample content, into your space in seconds - you just need to register for a Contentful account and setup a space. No credit card required.
4652

4753
### Gulp Tasks
4854

@@ -88,7 +94,6 @@ Some things to know:
8894
<img src="http://whobrokethebuild.me/wp-content/uploads/images/angular-contentful-starter.gif" />
8995

9096

91-
9297
### Adding AngularJS Libraries
9398

9499
The project uses one of the node packages to install libraries - bower. The process is simple - run the bower command specifying the library (and optional version) with the save command which will download the library into our lib folder which we can reference.
@@ -121,5 +126,25 @@ If you remove a library:
121126
* Remove any references in the application or in index.dev.html
122127

123128

129+
### Common Setup Problems
130+
131+
If you run into issues with npm and are seeing weird errors - won't worry, it's unfortunately all too common! The first thing I usually do is delete the node_modules and run an 'npm install'. Keep in mind that folder is not checked into the repository, and is easily rebuilt from the information in package.json.
132+
133+
* Delete node_modules in project
134+
* run 'npm cache clean'
135+
* run 'npm install'
136+
137+
If that doesn't work, I usually assume it's a node.js (or its module npm) versioning problems. Keep in mind npm is a component of node.js, so first verify your node version first:
138+
139+
```shell
140+
# Check the node version
141+
node -v
142+
143+
# Check the npm version
144+
npm -v
145+
```
146+
147+
At the time of writing this, I am on **node.js v8.9.4** and **npm v5.6.0** which are the latest production releases. If you still have problems, check out my blog post about wiping and reinstalling node & npm fresh:
124148

149+
[http://whobrokethebuild.me/fixing-broken-node-install-windows/](http://whobrokethebuild.me/fixing-broken-node-install-windows/)
125150

bin/contentful-export-import.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Imports
2+
var prompt = require('prompt');
3+
var spaceExport = require('contentful-export');
4+
var spaceImport = require('contentful-import');
5+
var updateJson = require('update-json');
6+
7+
// Contentful Export & Import Options
8+
var options = {
9+
spaceId: 'hpty8kufn7nl',
10+
managementToken: '6df4dff3fe45d44c7081be038af56a91ff8e9490c69c7649701fcf28eeb547e4',
11+
saveFile: false,
12+
maxAllowedItems: 100
13+
}
14+
15+
// Package JSON relative path.
16+
var filePath = './package.json';
17+
18+
// Contentful options parameter definitions.
19+
var schema = {
20+
properties: {
21+
// Contentful Space ID
22+
spaceId: {
23+
required: true
24+
},
25+
// Contentful Token
26+
managementToken: {
27+
required: true
28+
}
29+
}
30+
};
31+
32+
// Configuration data that we'll inject into package.json
33+
var data = {
34+
config: {
35+
contentfulConfigurations: {
36+
spaceId: options.spaceId,
37+
managementToken: options.managementToken
38+
}
39+
}
40+
}
41+
42+
43+
prompt.start();
44+
console.log("Starting the Contentful Export & Import Process..");
45+
console.log("Please enter your Contentful Space ID & Management Token.");
46+
console.log("You can find those values in Contentful under your space.");
47+
console.log("---------------------------------------------------------");
48+
49+
// Get two properties from the user: username and email
50+
prompt.get(schema, function (err, result) {
51+
spaceExport(options)
52+
.then((output) => {
53+
54+
// Update the options with the output JSON from the export and the user input spaceId & management token.
55+
options.content = output;
56+
options.spaceId = result.spaceId;
57+
options.managementToken = result.managementToken;
58+
59+
spaceImport(options)
60+
.then((output) => {
61+
console.log('Data Imported successfully');
62+
updateJson(filePath, data, function (error) {
63+
if (error) {
64+
console.log("An error occurred updating package.json: " + err);
65+
}
66+
});
67+
})
68+
.catch((err) => {
69+
console.log('Something went wrong with the import: ', err)
70+
})
71+
})
72+
.catch((err) => {
73+
console.log('Uh oh! Something went wrong:', err)
74+
})
75+
});

gulpfile.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ var gulp = require("gulp"),
1111
minifyHtml = require("gulp-minify-html"),
1212
replace = require("gulp-replace"),
1313
run = require("gulp-run"),
14-
connect = require("gulp-connect");
15-
sass = require('gulp-sass');
14+
connect = require("gulp-connect"),
15+
sass = require('gulp-sass'),
16+
gulpNgConfig = require('gulp-ng-config');
17+
1618

1719
var pkg = require("./package.json"),
1820
cssFile = "index.css", // CSS page name
@@ -28,7 +30,7 @@ var banner = [ "/* " + pkg.name + " v" + pkg.version + " " + dateformat(new Date
2830
},
2931
ngModule = pkg.name;
3032

31-
gulp.task("build", sync.sync([ ["css", "js", "tmpl", "bower.json"],
33+
gulp.task("build", sync.sync([ ["css", "js", "tmpl", "bower.json", "generateConfig"],
3234
pages.map(function(page) { return page + ".dev.html"; }), // Build page sources
3335
pages.map(function(page) { return page + ".html"; }) // Build release pages
3436
]));
@@ -72,20 +74,22 @@ gulp.task("tmpl", function(done) {
7274

7375
// Watch the files for changes
7476
gulp.task("watch", function() { ["tmpl", "css", "js"]
75-
.concat(pages.map(function(page) { return page + ".dev.html"; }))
76-
.forEach(function(i) {
77-
gulp.watch(paths[i], function(i) {
78-
return function() {
79-
gulp.src(paths['css'])
80-
.pipe(sass())
81-
.pipe(concat(cssFile))
82-
.pipe(gulp.dest(root+"/src/"));
83-
84-
gulp.src(paths[i])
85-
.pipe(connect.reload());
86-
};
87-
}(i));
88-
});
77+
.concat(pages.map(function(page) { return page + ".dev.html"; }))
78+
.forEach(function(i) {
79+
gulp.watch(paths[i], function(i) {
80+
return function() {
81+
gulp.src(paths['css'])
82+
.pipe(sass())
83+
.pipe(concat(cssFile))
84+
.pipe(gulp.dest(root+"/src/"));
85+
86+
gulp.src(paths[i])
87+
.pipe(connect.reload());
88+
};
89+
}(i));
90+
});
91+
92+
// Start LiveReload
8993
connect.server({
9094
root: root,
9195
port: 9000,
@@ -100,6 +104,15 @@ gulp.task("bower.json", function(done) {
100104
.on("end", done);
101105
});
102106

107+
gulp.task('generate-config', function () {
108+
gulp.src('package.json')
109+
.pipe(gulpNgConfig('contentfulConfig', { environment: 'config.contentfulConfigurations' }))
110+
.pipe(concat('contentful-config.js'))
111+
.pipe(gulp.dest('src/config/'));
112+
});
113+
114+
115+
103116
gulp.task("update-npm", function(done) {
104117
var cmd = "sh -c './node_modules/npm-check-updates/bin/npm-check-updates -u'";
105118
run(cmd).exec().on("end", done);

0 commit comments

Comments
 (0)