|
10 | 10 | # (ignoring .git/), performs a “full sync” and replaces occurrences |
11 | 11 | # of the literal word "template" in files with the target’s basename. |
12 | 12 | # If a target already has content, performs a selective sync |
13 | | -# excluding package.json, README.md, and src/. |
| 13 | +# excluding package.json, README.md, and src/, then merges package.json. |
14 | 14 | # |
15 | 15 | # Usage: |
16 | 16 | # sync.sh my-new-project |
17 | 17 | # sync.sh ../foo ../bar |
18 | 18 |
|
19 | | -# Exit on error, undefined var, or pipe failure. |
| 19 | +# Exit immediately on: |
| 20 | +# - any command returning non-zero (set -e), |
| 21 | +# - use of unset variables (set -u), |
| 22 | +# - failure within a pipeline (set -o pipefail). |
20 | 23 | set -euo pipefail |
21 | 24 |
|
22 | | -# Ensure at least one target directory is specified. |
| 25 | +# Ensure at least one target directory is given. |
23 | 26 | if (( $# < 1 )); then |
24 | 27 | echo "Usage: $0 <target> [<target>…]" >&2 |
25 | 28 | exit 1 |
26 | 29 | fi |
27 | 30 |
|
28 | | -# Determine this script’s own directory and name, so we can rsync from here |
| 31 | +# Determine this script’s directory and name for rsync source. |
29 | 32 | script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
30 | 33 | script_name="$(basename "${BASH_SOURCE[0]}")" |
31 | 34 |
|
32 | | -# Collect and validate absolute target paths |
| 35 | +# Collect and validate absolute paths for each target. |
33 | 36 | targets=() |
34 | 37 | for t; do |
35 | | - mkdir -p "$t" # create if missing |
36 | | - abs_t="$(cd "$t" && pwd -P)" # resolve to absolute path |
37 | | - [[ ! -w $abs_t ]] && { # require write permission |
| 38 | + mkdir -p "$t" # create target if missing |
| 39 | + abs_t="$(cd "$t" && pwd -P)" # resolve to canonical absolute path |
| 40 | + |
| 41 | + # Require write permission on the target directory. |
| 42 | + if [[ ! -w $abs_t ]]; then |
38 | 43 | echo "Error: no write access to $abs_t" >&2 |
39 | 44 | exit 1 |
40 | | - } |
| 45 | + fi |
| 46 | + |
41 | 47 | targets+=("$abs_t") |
42 | 48 | done |
43 | 49 |
|
44 | 50 | # is_empty_dir <dir> |
45 | | -# Return 0 if <dir> contains no entries except maybe .git/, |
46 | | -# Return 1 otherwise. |
| 51 | +# Returns 0 (success) if <dir> contains no entries |
| 52 | +# except optionally a .git/ directory; returns 1 otherwise. |
47 | 53 | is_empty_dir() { |
48 | 54 | find "$1" -mindepth 1 \ |
49 | 55 | ! -path "$1/.git" \ |
50 | 56 | ! -path "$1/.git/*" \ |
51 | | - -print -quit | grep -q . \ |
52 | | - && return 1 \ |
53 | | - || return 0 |
| 57 | + -print -quit \ |
| 58 | + | grep -q . && return 1 || return 0 |
54 | 59 | } |
55 | 60 |
|
56 | | -# Base rsync options: archive mode, exclude this script and .git/, |
57 | | -# respect .gitignore filters. |
| 61 | +# Base rsync options: |
| 62 | +# --archive preserve permissions, timestamps, etc. |
| 63 | +# --exclude=script do not copy this sync script into targets |
| 64 | +# --exclude='.git/' skip Git meta directory |
| 65 | +# --filter=':- .gitignore' respect .gitignore rules |
58 | 66 | RSYNC_OPTS=( |
59 | 67 | --archive |
60 | 68 | --exclude="$script_name" |
61 | 69 | --exclude='.git/' |
62 | 70 | --filter=':- .gitignore' |
63 | 71 | ) |
64 | 72 |
|
65 | | -# Perform sync for each validated target |
| 73 | +# Loop over each validated target and perform sync. |
66 | 74 | for target in "${targets[@]}"; do |
67 | 75 | echo ">>> Syncing to $target" |
68 | 76 |
|
69 | 77 | if is_empty_dir "$target"; then |
70 | | - # Full initial sync: copy everything and replace "template" |
| 78 | + # Full initial sync: copy all files, then transform "template" placeholders. |
71 | 79 | echo " Empty dir -> full sync" |
72 | | - rsync "${RSYNC_OPTS[@]}" "$script_dir/." "$target" |
| 80 | + rsync "${RSYNC_OPTS[@]}" "$script_dir/." "$target/" |
73 | 81 |
|
| 82 | + # Replace every occurrence of the literal word "template" in each text file |
74 | 83 | base="$(basename "$target")" |
75 | | - # Replace occurrences of 'template' in all text files |
76 | 84 | find "$target" -type f -exec grep -Il . {} + | |
77 | 85 | while IFS= read -r file; do |
78 | 86 | sed -i "s/template/${base}/g" "$file" |
79 | 87 | done |
80 | 88 |
|
81 | 89 | else |
82 | | - # Selective sync: do not overwrite key project files |
| 90 | + # Selective sync: exclude key project files and directories. |
83 | 91 | echo " Non-empty dir -> selective sync" |
84 | 92 | rsync "${RSYNC_OPTS[@]}" \ |
85 | 93 | --exclude='package.json' \ |
86 | 94 | --exclude='README.md' \ |
87 | 95 | --exclude='src/**' \ |
88 | | - "$script_dir/." "$target" |
| 96 | + "$script_dir/." "$target/" |
| 97 | + |
| 98 | + # Merge package.json: keep existing fields but apply updated template defaults. |
| 99 | + old="$target/package.json" |
| 100 | + tmpl="$script_dir/package.json" |
| 101 | + merged="$target/package.json.tmp" |
| 102 | + base="$(basename "$target")" |
| 103 | + |
| 104 | + jq -n \ |
| 105 | + --slurpfile new <(sed "s/template/${base}/g" "$tmpl") \ |
| 106 | + --slurpfile old "$old" \ |
| 107 | + ' |
| 108 | + $new[0] as $new | |
| 109 | + $old[0] as $old | |
| 110 | +
|
| 111 | + # Start with full template structure, then override preserved fields. |
| 112 | + $new |
| 113 | + | .name = $old.name |
| 114 | + | .version = $old.version |
| 115 | + | .description = $old.description |
| 116 | + | .dependencies = ($new.dependencies + $old.dependencies) |
| 117 | + | .devDependencies = ($new.devDependencies + $old.devDependencies) |
| 118 | + | .scripts = ($new.scripts + $old.scripts) |
| 119 | + ' > "$merged" \ |
| 120 | + && mv "$merged" "$old" |
89 | 121 | fi |
90 | 122 | done |
0 commit comments