Skip to content

Commit

Permalink
account for the extremely helpful and intuitive npm behaviour - fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 22, 2021
1 parent 32b3fcd commit f249c0c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-students-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Add .gitignore files to new projects
10 changes: 5 additions & 5 deletions packages/create-svelte/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function main() {
mkdirp(cwd);
}

const options = /** @type {import('../types/internal').Options} */ (await prompts([
const options = /** @type {import('./types/internal').Options} */ (await prompts([
{
type: 'select',
name: 'template',
Expand Down Expand Up @@ -145,11 +145,11 @@ async function main() {
*/
function write_template_files(template, typescript, name, cwd) {
const dir = dist(`templates/${template}`);
copy(`${dir}/assets`, cwd);
copy(`${dir}/assets`, cwd, (name) => name.replace('gitignore', '.gitignore'));
copy(`${dir}/package.json`, `${cwd}/package.json`);

const manifest = `${dir}/files.${typescript ? 'ts' : 'js'}.json`;
const files = /** @type {import('../types/internal').File[]} */ (JSON.parse(
const files = /** @type {import('./types/internal').File[]} */ (JSON.parse(
fs.readFileSync(manifest, 'utf-8')
));

Expand All @@ -164,11 +164,11 @@ function write_template_files(template, typescript, name, cwd) {
/**
*
* @param {string} cwd
* @param {import('../types/internal').Options} options
* @param {import('./types/internal').Options} options
*/
function write_common_files(cwd, options) {
const shared = dist('shared.json');
const { files } = /** @type {import('../types/internal').Common} */ (JSON.parse(
const { files } = /** @type {import('./types/internal').Common} */ (JSON.parse(
fs.readFileSync(shared, 'utf-8')
));

Expand Down
1 change: 1 addition & 0 deletions packages/create-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@sveltejs/kit": "workspace:*",
"@types/gitignore-parser": "^0.0.0",
"@types/prettier": "^2.2.3",
"@types/prompts": "^2.0.10",
"gitignore-parser": "^0.0.2",
"prettier": "^2.2.1",
"prettier-plugin-svelte": "^2.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/create-svelte/scripts/build-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function generate_templates(shared) {
// ignore contents of .gitignore or .ignore
if (!gitignore.accepts(name) || !ignore.accepts(name) || name === '.ignore') return;

// the package.template.json thing is a bit annoying — basically we want
// the package.template.json thing is a bit annoying — basically we want
// to be able to develop and deploy the app from here, but have a different
// package.json in newly created projects (based on package.template.json)
if (/\.(js|ts|svelte|svelte\.md)$/.test(name) || name === 'package.template.json') {
Expand All @@ -55,7 +55,7 @@ async function generate_templates(shared) {
});
}
} else {
const dest = path.join(assets, name);
const dest = path.join(assets, name).replace('.gitignore', 'gitignore'); // npm does wacky stuff to gitignores
mkdirp(path.dirname(dest));
fs.copyFileSync(path.join(cwd, name), dest);
}
Expand Down
21 changes: 12 additions & 9 deletions packages/create-svelte/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ export function rimraf(path) {
(fs.rmSync || fs.rmdirSync)(path, { recursive: true, force: true });
}

/**
* @template T
* @param {T} x
*/
function identity(x) {
return x;
}

/**
* @param {string} from
* @param {string} to
* @param {(basename: string) => boolean} filter
* @param {(basename: string) => string} rename
*/
export function copy(from, to, filter = () => true) {
if (!fs.existsSync(from)) return [];
if (!filter(path.basename(from))) return [];
export function copy(from, to, rename = identity) {
if (!fs.existsSync(from)) return;

const files = [];
const stats = fs.statSync(from);

if (stats.isDirectory()) {
fs.readdirSync(from).forEach((file) => {
files.push(...copy(path.join(from, file), path.join(to, file)));
copy(path.join(from, file), path.join(to, rename(file)));
});
} else {
mkdirp(path.dirname(to));
fs.copyFileSync(from, to);
files.push(to);
}

return files;
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit f249c0c

Please sign in to comment.