Skip to content

Commit

Permalink
fix: Installation script crash (#66)
Browse files Browse the repository at this point in the history
## Description

This PR:
- fixes incorrect commands in readme,
- updates husky setup (was deprecated),
- is an attempt to fix `npx rn-lib-temp` command run (hope it will work)
  • Loading branch information
MatiPl01 authored Nov 16, 2024
1 parent b19c4e5 commit 957d57e
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ template/js
!.yarn/releases
!.yarn/plugins
.pnp.*

*.tgz
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
6 changes: 6 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
"changelogFile": "CHANGELOG.md"
}
],
[
"@semantic-release/exec",
{
"prepareCmd": "./scripts/create-npm-package.sh"
}
],
"@semantic-release/npm",
[
"@semantic-release/git",
Expand Down
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ Before starting development, go to the directory where you created the template
yarn
```

**For iOS only**: Install required Pods for the bare React Native example app:
**For iOS only**: Install required Pods for the bare React Native example app (`examples/fabric` or `examples/paper`):

```sh
yarn example:bare pod
```

### Getting help

For more details about the `npx rn-lib-temp` command use the following command:

```sh
npx rn-lib-temp help
yarn pod
```

## 💫 GitHub Actions
Expand Down Expand Up @@ -99,13 +91,13 @@ If you don't need to include **expo** or **fabric**/**paper** React Native app e
For launching the bare React Native example app:

```sh
yarn example:bare start|android|ios|pod
yarn start|android|ios|pod
```

For the Expo React Native app:

```sh
yarn example:expo start|android|ios
yarn start|android|ios
```

- `start` - starts metro client
Expand Down
11 changes: 8 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ yargs(hideBin(process.argv))
init(argv.projectName, argv.verbose, argv.directory);
}
)
.demandCommand(1, 'You need at least one command before moving on')
.demandCommand(1, '', 'You need at least one command before moving on')
.help()
.fail(msg => {
logger.error(msg);
.fail((msg, err, yargsInstance) => {
if (!msg && !err) {
logger.error('You need at least one command before moving on.');
console.log(yargsInstance.help());
} else {
logger.error(msg || err.message);
}
process.exit(1);
})
.strict().argv;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@eslint/js": "^9.1.1",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^13.0.0",
"@semantic-release/exec": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^10.0.7",
"@semantic-release/npm": "^12.0.1",
Expand All @@ -22,7 +23,7 @@
"eslint": "^8.57.0",
"eslint-plugin-prettier": "^5.1.3",
"globals": "^15.0.0",
"husky": "^9.0.11",
"husky": "^9.1.6",
"lint-staged": "^15.2.7",
"prettier": "^3.3.2",
"semantic-release": "^24.0.0",
Expand Down Expand Up @@ -83,7 +84,7 @@
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"postinstall": "yarn library:install",
"prepare": "husky install"
"prepare": "husky"
},
"type": "module"
}
41 changes: 41 additions & 0 deletions scripts/create-npm-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

PACKAGE_JSON="package.json"
PACKAGE_JSON_BAK="package.json.bak"

# Check if package.json exists
if [ ! -f "$PACKAGE_JSON" ]; then
echo "Error: $PACKAGE_JSON not found."
exit 1
fi

# Backup the original package.json
cp "$PACKAGE_JSON" "$PACKAGE_JSON_BAK"

# Execute the Node.js script to modify package.json
node ./scripts/remove-scripts.js

# Check if the Node.js script executed successfully
if [ $? -ne 0 ]; then
echo "Error: Failed to modify package.json."
# Restore the original package.json before exiting
mv "$PACKAGE_JSON_BAK" "$PACKAGE_JSON"
exit 1
fi

# Run npm pack
npm pack

# Capture the exit status of npm pack
PACK_STATUS=$?

# Restore the original package.json
mv "$PACKAGE_JSON_BAK" "$PACKAGE_JSON"

# Check if npm pack was successful
if [ $PACK_STATUS -ne 0 ]; then
echo "Error: npm pack failed."
exit 1
fi

echo "Successfully packed the package and restored the original package.json."
43 changes: 43 additions & 0 deletions scripts/remove-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env node

import fs from 'fs';
import chalk from 'chalk';

const PACKAGE_JSON = 'package.json';

let packageJson;
try {
packageJson = JSON.parse(fs.readFileSync(PACKAGE_JSON, 'utf8'));
} catch (error) {
console.error(chalk.red('Error reading package.json:'), error);
process.exit(1);
}

// Remove the postinstall and prepare scripts if they exist
if (packageJson.scripts) {
const scriptsRemoved = [];
if (packageJson.scripts.postinstall) {
delete packageJson.scripts.postinstall;
scriptsRemoved.push('postinstall');
}
if (packageJson.scripts.prepare) {
delete packageJson.scripts.prepare;
scriptsRemoved.push('prepare');
}
if (scriptsRemoved.length > 0) {
console.log(
chalk.green('Removed scripts: ') + chalk.yellow(scriptsRemoved.join(', '))
);
} else {
console.log(chalk.blue('No scripts to remove.'));
}
} else {
console.log(chalk.blue('No scripts section found in package.json.'));
}

try {
fs.writeFileSync(PACKAGE_JSON, JSON.stringify(packageJson, null, 2));
} catch (error) {
console.error(chalk.red('Error writing package.json:'), error);
process.exit(1);
}
3 changes: 0 additions & 3 deletions template/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
4 changes: 2 additions & 2 deletions template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
"format:check": "prettier --check . --ignore-unknown",
"format:code": "prettier --write . --ignore-unknown",
"format:deps": "syncpack format",
"husky:install": "./scripts/husky.sh",
"husky:init": "./scripts/husky.sh",
"lib": "yarn workspace __library-name__",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"postinstall": "yarn husky:install",
"postinstall": "yarn husky:init",
"prepare": "husky",
"test": "yarn lib test && yarn workspace example-app test",
"typecheck": "yarn lib typecheck && yarn workspace example-app typecheck"
Expand Down
2 changes: 1 addition & 1 deletion template/scripts/husky.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
if [ -d '.git' ]; then
npx husky install
npx husky init
fi
23 changes: 20 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,22 @@ __metadata:
languageName: node
linkType: hard

"@semantic-release/exec@npm:^6.0.3":
version: 6.0.3
resolution: "@semantic-release/exec@npm:6.0.3"
dependencies:
"@semantic-release/error": "npm:^3.0.0"
aggregate-error: "npm:^3.0.0"
debug: "npm:^4.0.0"
execa: "npm:^5.0.0"
lodash: "npm:^4.17.4"
parse-json: "npm:^5.0.0"
peerDependencies:
semantic-release: ">=18.0.0"
checksum: 10c0/87c1f5dcd96e8b51cfa084ff2fd570fa1e2b6368064fba54403797636279e1159b62c199b3e8219d31189acfe8aa6f1b596f6cda6508fd3e1a7e5739c86a1a65
languageName: node
linkType: hard

"@semantic-release/git@npm:^10.0.1":
version: 10.0.1
resolution: "@semantic-release/git@npm:10.0.1"
Expand Down Expand Up @@ -2478,7 +2494,7 @@ __metadata:
languageName: node
linkType: hard

"husky@npm:^9.0.11":
"husky@npm:^9.1.6":
version: 9.1.6
resolution: "husky@npm:9.1.6"
bin:
Expand Down Expand Up @@ -4166,7 +4182,7 @@ __metadata:
languageName: node
linkType: hard

"parse-json@npm:^5.2.0":
"parse-json@npm:^5.0.0, parse-json@npm:^5.2.0":
version: 5.2.0
resolution: "parse-json@npm:5.2.0"
dependencies:
Expand Down Expand Up @@ -4666,6 +4682,7 @@ __metadata:
"@eslint/js": "npm:^9.1.1"
"@semantic-release/changelog": "npm:^6.0.3"
"@semantic-release/commit-analyzer": "npm:^13.0.0"
"@semantic-release/exec": "npm:^6.0.3"
"@semantic-release/git": "npm:^10.0.1"
"@semantic-release/github": "npm:^10.0.7"
"@semantic-release/npm": "npm:^12.0.1"
Expand All @@ -4676,7 +4693,7 @@ __metadata:
eslint: "npm:^8.57.0"
eslint-plugin-prettier: "npm:^5.1.3"
globals: "npm:^15.0.0"
husky: "npm:^9.0.11"
husky: "npm:^9.1.6"
lint-staged: "npm:^15.2.7"
prettier: "npm:^3.3.2"
semantic-release: "npm:^24.0.0"
Expand Down

0 comments on commit 957d57e

Please sign in to comment.