Skip to content

Commit

Permalink
fix: workaround for npm ignoring .gitignore (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Nov 30, 2022
1 parent b5c0844 commit f05935e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"/android/app/src/!(test)",
"/android/support/src",
"/common",
"/example/.gitignore",
"/example/_gitignore",
"/example/android/gradle*",
"/example/windows/.gitignore",
"/example/windows/_gitignore",
"/example/{metro,react-native}.config.js",
"/ios",
"/macos",
Expand Down Expand Up @@ -64,6 +64,8 @@
"lint:kt": "ktlint --android --relative 'android/app/src/**/*.kt'",
"lint:rb": "bundle exec rubocop",
"lint:swift": "swiftlint",
"prepack": "node scripts/pack.mjs pre",
"postpack": "node scripts/pack.mjs post",
"set-react-version": "node scripts/set-react-version.mjs",
"test:js": "jest",
"test:rb": "bundle exec ruby -Ilib:test -e \"Dir.glob('test/test_*.rb').each { |file| require_relative file }\""
Expand Down
12 changes: 5 additions & 7 deletions scripts/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ const getConfig = (() => {
typeof configuration === "undefined" ||
"JEST_WORKER_ID" in process.env // skip caching when testing
) {
// Git ignore files are only renamed when published. `GIT_IGNORE_FILE`
// should therefore only be set during testing.
const gitignore = process.env["GIT_IGNORE_FILE"] || "_gitignore";
const { name, templatePath, testAppPath, flatten, init } = params;
const projectPathFlag = flatten ? " --project-path ." : "";
const testAppRelPath = projectRelativePath(params);
Expand All @@ -421,7 +424,7 @@ const getConfig = (() => {
common: {
files: {
".gitignore": {
source: path.join(testAppPath, "example", ".gitignore"),
source: path.join(testAppPath, "example", gitignore),
},
".watchmanconfig": {
source: path.join(templateDir, "_watchmanconfig"),
Expand Down Expand Up @@ -631,12 +634,7 @@ const getConfig = (() => {
windows: {
files: {
".gitignore": {
source: path.join(
testAppPath,
"example",
"windows",
".gitignore"
),
source: path.join(testAppPath, "example", "windows", gitignore),
},
},
oldFiles: [
Expand Down
2 changes: 1 addition & 1 deletion scripts/install-test-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ done
npm pack

yarn
npx react-native init-test-app --destination template-example --name TemplateExample --platform "$platform"
GIT_IGNORE_FILE=".gitignore" npx react-native init-test-app --destination template-example --name TemplateExample --platform "$platform"

pushd template-example 1> /dev/null

Expand Down
41 changes: 41 additions & 0 deletions scripts/pack.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-check
import * as fs from "fs";
import * as path from "path";

const files = ["example/.gitignore", "example/windows/.gitignore"];

/**
* Renames `.dotfile` to `_dotfile`.
* @param {string} p
* @returns {string}
*/
function renameDotFile(p) {
return path.join(path.dirname(p), "_" + path.basename(p).substring(1));
}

/**
* `npm install` seems to be renaming `.gitignore` files to `.npmignore`.
* This breaks our templates (see
* https://github.com/microsoft/react-native-test-app/issues/1228). As a
* workaround, we'll rename the files ourselves, from `.gitignore` to
* `_gitignore`, to avoid any further attempts to mutate the package.
*/
const { [2]: script } = process.argv;
switch (script) {
case "pre": {
for (const f of files) {
fs.renameSync(f, renameDotFile(f));
}
break;
}

case "post": {
for (const f of files) {
fs.renameSync(renameDotFile(f), f);
}
break;
}

default:
throw new Error(`No such script: ${script}`);
}
5 changes: 5 additions & 0 deletions test/android-test-app/test-app-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ describe("test-app-util", () => {
return Number(major) * 10000 + Number(minor) * 100 + Number(patch);
}

beforeAll(() => {
process.env["GIT_IGNORE_FILE"] = ".gitignore";
});

afterAll(() => {
delete process.env["GIT_IGNORE_FILE"];
removeProject(defaultTestProject);
});

Expand Down
16 changes: 8 additions & 8 deletions test/configure/__snapshots__/gatherConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Object {
"dependencies": Object {},
"files": Object {
".gitignore": Object {
"source": "example/.gitignore",
"source": "example/_gitignore",
},
".watchmanconfig": Object {
"source": "node_modules/react-native/template/_watchmanconfig",
Expand Down Expand Up @@ -64,7 +64,7 @@ Object {
"dependencies": Object {},
"files": Object {
".gitignore": Object {
"source": "example/.gitignore",
"source": "example/_gitignore",
},
".watchmanconfig": Object {
"source": "node_modules/react-native/template/_watchmanconfig",
Expand Down Expand Up @@ -195,7 +195,7 @@ Object {
"dependencies": Object {},
"files": Object {
".gitignore": Object {
"source": "example/.gitignore",
"source": "example/_gitignore",
},
".watchmanconfig": Object {
"source": "node_modules/react-native/template/_watchmanconfig",
Expand All @@ -204,7 +204,7 @@ Object {
"source": "node_modules/react-native/template/babel.config.js",
},
"common/.gitignore": Object {
"source": "example/.gitignore",
"source": "example/_gitignore",
},
"common/.watchmanconfig": Object {
"source": "node_modules/react-native/template/_watchmanconfig",
Expand Down Expand Up @@ -293,7 +293,7 @@ Object {
"dependencies": Object {},
"files": Object {
".gitignore": Object {
"source": "example/.gitignore",
"source": "example/_gitignore",
},
".watchmanconfig": Object {
"source": "node_modules/react-native/template/_watchmanconfig",
Expand Down Expand Up @@ -366,7 +366,7 @@ Object {
},
"files": Object {
".gitignore": Object {
"source": "example/.gitignore",
"source": "example/_gitignore",
},
".watchmanconfig": Object {
"source": "node_modules/react-native/template/_watchmanconfig",
Expand Down Expand Up @@ -482,7 +482,7 @@ module.exports = {
};
",
"windows/.gitignore": Object {
"source": "example/windows/.gitignore",
"source": "example/windows/_gitignore",
},
},
"oldFiles": Array [
Expand Down Expand Up @@ -517,7 +517,7 @@ Object {
"dependencies": Object {},
"files": Object {
".gitignore": Object {
"source": "example/.gitignore",
"source": "example/_gitignore",
},
".watchmanconfig": Object {
"source": "node_modules/react-native/template/_watchmanconfig",
Expand Down

0 comments on commit f05935e

Please sign in to comment.