Skip to content

Commit

Permalink
feat: support .gitignore files in generated app
Browse files Browse the repository at this point in the history
It is impossible to deploy `.gitignore` files via npm packages.
Instead, Cordova templates should include `gitignore` files that we
rename to `.gitignore`. This commit implements that renaming.

For more details see apache/cordova-discuss#69.

Closes apache#8 and cordova-app-hello-world#30
  • Loading branch information
raphinesse committed Nov 15, 2019
1 parent b64df2e commit 0fa8bb8
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 5 deletions.
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var path = require('path');

var tmp = require('tmp');
const npa = require('npm-package-arg');
const globby = require('globby');
var isObject = require('isobject');
var pathIsInside = require('path-is-inside');
var requireFresh = require('import-fresh');
Expand Down Expand Up @@ -126,7 +127,22 @@ function cordovaCreate (dest, opts = {}) {
}
throw e;
}

})
.then(async () => {
// It is impossible to deploy .gitignore files via npm packages.
// Instead, Cordova templates should include gitignore files that we
// rename to .gitignore here. For more details see
// https://github.com/apache/cordova-discuss/issues/69
const undottedFiles = await globby(['**/gitignore'], {
cwd: dir,
absolute: true
});

await Promise.all(undottedFiles.map(f =>
fs.move(f, path.join(path.dirname(f), '.gitignore'))
));
})
.then(() => {
// Write out id, name and version to config.xml
const configPath = path.join(dir, 'config.xml');
const conf = new ConfigParser(configPath);
Expand Down
178 changes: 174 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cordova-common": "^3.1.0",
"cordova-fetch": "^2.0.0",
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"import-fresh": "^3.1.0",
"isobject": "^4.0.0",
"npm-package-arg": "^6.1.1",
Expand Down
27 changes: 27 additions & 0 deletions spec/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ describe('create end-to-end', function () {
return create(project, opts)
.then(checkProjectCreatedWithDefaultTemplate);
});

it('should rename all gitignore files in template to .gitignore', () => {
const baseTemplatePkg = path.join(__dirname, 'templates/withsubdirectory');
const templatePkg = path.join(tmpDir, 'gitignore-template');
fs.copySync(baseTemplatePkg, templatePkg);

// Setup a few gitignore files that should be renamed (or not)
const templateDir = path.join(templatePkg, 'template');
fs.ensureFileSync(path.join(templateDir, 'gitignore'));
fs.ensureFileSync(path.join(templateDir, 'www/gitignore'));
fs.ensureDirSync(path.join(templateDir, 'foo/gitignore'));

opts.template = templatePkg;
return create(project, opts).then(() => {
// Renames gitignore at template root
expect(path.join(project, 'gitignore')).not.toExist();
expect(path.join(project, '.gitignore')).toExist();

// Renames gitignores in sub-directories
expect(path.join(project, 'www/gitignore')).not.toExist();
expect(path.join(project, 'www/.gitignore')).toExist();

// Does not rename directories with name gitignore
expect(path.join(project, 'foo/gitignore')).toExist();
expect(path.join(project, 'foo/.gitignore')).not.toExist();
});
});
});

describe('when shit happens', function () {
Expand Down

0 comments on commit 0fa8bb8

Please sign in to comment.