Skip to content

Commit b5e4f91

Browse files
authored
fix: handle package.json content as string (#40)
1 parent 328de7e commit b5e4f91

5 files changed

Lines changed: 92 additions & 91 deletions

File tree

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let package = Package(
4848
dependencies: [
4949
.target(name: "JavascriptPackageTools"),
5050
],
51-
resources: [.copy("../Resources/package-new.json")]
51+
resources: [.copy("../Resources/package-new.json"), .copy("../Resources/package-old.json")]
5252
),
5353
.testTarget(name: "CapacitorPluginSyntaxToolsTests",
5454
dependencies: [

Sources/CapacitorPluginTools/CapacitorPluginPackage.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,7 @@ public class CapacitorPluginPackage {
147147
try? packageJSONParser.changeScript(named: "verify:ios",
148148
to: "xcodebuild -scheme \(podName) -destination generic/platform=iOS")
149149

150-
var newFiles = packageJSONParser.files
151-
152-
newFiles.removeAll(where: { $0 == "ios/Plugin" || $0 == "ios/Plugin/" })
153-
154-
if !newFiles.contains(where: { $0 == "ios/"}) {
155-
newFiles.append("ios/Sources")
156-
newFiles.append("ios/Tests")
157-
}
158-
159-
newFiles.append("Package.swift")
160-
161-
packageJSONParser.files = newFiles
150+
packageJSONParser.setFiles()
162151

163152
try packageJSONParser.writePackageJSON()
164153
}

Sources/JavascriptPackageTools/PackageJSONParser.swift

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ public enum PackageJSONError: Error {
1010

1111
public struct PackageJSONParser: CustomDebugStringConvertible {
1212
private let package: PackageJSON
13-
private var json: JSON
13+
private let json: JSON
1414
private let jsonURL: URL
15+
public var jsonString: String
1516

1617
public var npmName: String {
1718
package.name
@@ -22,12 +23,7 @@ public struct PackageJSONParser: CustomDebugStringConvertible {
2223
}
2324

2425
public var files: [String] {
25-
get {
26-
package.files
27-
}
28-
set {
29-
json["files"] = JSON(newValue)
30-
}
26+
package.files
3127
}
3228

3329
public var podspec: String = ""
@@ -47,35 +43,53 @@ public struct PackageJSONParser: CustomDebugStringConvertible {
4743

4844
return plugins
4945
}
50-
51-
public var jsonString: String? {
52-
json.rawString(.utf8, options: [.withoutEscapingSlashes, .prettyPrinted])
53-
}
5446

5547
public init(with url: URL) throws {
5648
jsonURL = url
5749

5850
let data = try Data(contentsOf: url)
5951
json = try JSON(data: data)
60-
52+
jsonString = try String(contentsOf: url, encoding: .utf8)
6153
package = try JSONDecoder().decode(PackageJSON.self, from: data)
6254
podspec = try findPodspec()
6355
}
6456

6557
public mutating func changeScript(named: String, to runString: String) throws(PackageJSONError) {
6658
if json["scripts"][named] != JSON.null {
67-
json["scripts"][named] = JSON(runString)
59+
jsonString = jsonString.replacingOccurrences(of: json["scripts"][named].stringValue, with: runString)
6860
} else {
6961
throw .scriptEntryNotFound
7062
}
7163
}
7264

73-
public func writePackageJSON() throws {
74-
guard let data = jsonString?.data(using: .utf8) else {
75-
throw PackageJSONError.jsonStringGenerationFailed
65+
public mutating func setFiles() {
66+
var replacements: [String] = ["ios/Plugin", "ios/Plugin/"]
67+
68+
var newFiles: String = """
69+
"Package.swift",
70+
"""
71+
72+
if !files.contains(where: { $0 == "ios/"}) {
73+
newFiles = """
74+
"ios/Sources",
75+
"ios/Tests",
76+
\(newFiles)
77+
"""
78+
} else {
79+
replacements.append("ios/")
80+
newFiles = """
81+
"ios/",
82+
\(newFiles)
83+
"""
7684
}
77-
78-
try data.write(to: jsonURL)
85+
86+
replacements.forEach {replacement in
87+
jsonString = jsonString.replacingOccurrences(of: "\"\(replacement)\",", with: newFiles)
88+
}
89+
}
90+
91+
public func writePackageJSON() throws {
92+
try jsonString.write(to: jsonURL, atomically: true, encoding: .utf8)
7993
}
8094

8195
public var debugDescription: String {

Tests/JavascriptPackageToolsTests/PackageJSONParserTests.swift

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,44 @@ import Foundation
33
import JavascriptPackageTools
44

55
struct PackageJSONParserTests {
6-
let packageJSONParser: PackageJSONParser
6+
let newPackageJSONParser: PackageJSONParser
7+
let oldPackageJSONParser: PackageJSONParser
78

89
init() throws {
9-
let testJSON = try #require(Bundle.module.url(forResource: "package-new", withExtension: "json"))
10-
packageJSONParser = try PackageJSONParser(with: testJSON)
10+
let newJSON = try #require(Bundle.module.url(forResource: "package-new", withExtension: "json"))
11+
let oldJSON = try #require(Bundle.module.url(forResource: "package-old", withExtension: "json"))
12+
newPackageJSONParser = try PackageJSONParser(with: newJSON)
13+
oldPackageJSONParser = try PackageJSONParser(with: oldJSON)
1114
}
1215

1316
@Test("Correctly finds the podspec")
1417
func findsPodSpec() async throws {
15-
#expect(packageJSONParser.podspec == "Typical.podspec")
18+
#expect(newPackageJSONParser.podspec == "Typical.podspec")
1619
}
1720

1821
@Test("Can change scripts")
1922
func canChangeScript() async throws {
20-
var parser = packageJSONParser
21-
let oldString = try #require(parser.jsonString)
22-
try parser.changeScript(named: "verify:ios", to: "new-test")
23-
let calculatedSting = try #require(parser.jsonString)
23+
var parser = oldPackageJSONParser
24+
let oldString = parser.jsonString
25+
try parser.changeScript(named: "verify:ios", to: "xcodebuild build -scheme TypicalPlugin -destination generic/platform=iOS")
26+
let calculatedSting = parser.jsonString
2427
#expect(calculatedSting != oldString)
2528
}
2629

2730
@Test("Can change files")
2831
func canSetFiles() async throws {
29-
var parser = packageJSONParser
30-
let oldString = try #require(parser.jsonString)
31-
parser.files = ["new", "list", "files"]
32-
let calculatedSting = try #require(parser.jsonString)
32+
var parser = oldPackageJSONParser
33+
let oldString = parser.jsonString
34+
parser.setFiles()
35+
let calculatedSting = parser.jsonString
3336
#expect(calculatedSting != oldString)
3437
}
38+
39+
@Test("Full package update")
40+
func replacedFilesAndScripts() async throws {
41+
var oldParser = oldPackageJSONParser
42+
try oldParser.changeScript(named: "verify:ios", to: "xcodebuild build -scheme TypicalPlugin -destination generic/platform=iOS")
43+
oldParser.setFiles()
44+
#expect(newPackageJSONParser.jsonString == oldParser.jsonString)
45+
}
3546
}

Tests/Resources/package-old.json

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,74 @@
11
{
2-
"name": "@capacitor/google-maps",
3-
"version": "7.0.2",
4-
"description": "Google maps on Capacitor",
2+
"name": "@capacitor/typicalplugin",
3+
"version": "7.0.1",
4+
"description": "Does some things",
55
"main": "dist/plugin.cjs.js",
66
"module": "dist/esm/index.js",
7-
"typings": "dist/typings/index.d.ts",
8-
"typesVersions": {
9-
"<4.1": {
10-
"dist/typings/index.d.ts": [
11-
"dist/typings/ts_old/index.d.ts"
12-
]
13-
}
14-
},
7+
"types": "dist/esm/index.d.ts",
158
"unpkg": "dist/plugin.js",
169
"files": [
1710
"android/src/main/",
1811
"android/build.gradle",
1912
"dist/",
2013
"ios/Plugin/",
21-
"CapacitorGoogleMaps.podspec"
14+
"Typical.podspec"
2215
],
2316
"author": "Ionic <hi@ionicframework.com>",
2417
"license": "MIT",
2518
"repository": {
2619
"type": "git",
27-
"url": "git+https://github.com/ionic-team/capacitor-google-maps.git"
20+
"url": "git+https://github.com/ionic-team/capacitor-plugins.git"
2821
},
2922
"bugs": {
30-
"url": "https://github.com/ionic-team/capacitor-google-maps/issues"
23+
"url": "https://github.com/ionic-team/capacitor-plugins/issues"
3124
},
3225
"keywords": [
3326
"capacitor",
3427
"plugin",
35-
"native",
36-
"google-maps"
28+
"native"
3729
],
3830
"scripts": {
39-
"verify": "pnpm run verify:ios && pnpm run verify:android && pnpm run verify:web",
31+
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
4032
"verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -sdk iphonesimulator && cd ..",
4133
"verify:android": "cd android && ./gradlew clean build test && cd ..",
42-
"verify:web": "pnpm run build",
43-
"lint": "pnpm eslint . --ext ts && pnpm prettier \"./**/*.{css,html,ts,js,java}\" --check && pnpm node-swiftlint lint",
44-
"fmt": "pnpm eslint . --ext ts --fix && pnpm prettier \"./**/*.{css,html,ts,js,java}\" --write && pnpm node-swiftlint --fix --format",
45-
"docgen": "docgen --api GoogleMapInterface --output-readme README.md --output-json dist/docs.json",
46-
"build": "pnpm run clean && pnpm run docgen && tsc && rollup -c rollup.config.js && pnpm run downleveldts",
34+
"verify:web": "npm run build",
35+
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
36+
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
37+
"eslint": "eslint . --ext ts",
38+
"prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
39+
"swiftlint": "node-swiftlint",
40+
"docgen": "docgen --api TypicalPlugin --output-readme README.md --output-json dist/docs.json",
41+
"build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
4742
"clean": "rimraf ./dist",
4843
"watch": "tsc --watch",
49-
"prepublishOnly": "pnpm run build",
50-
"publish:cocoapod": "pod trunk push ./CapacitorGoogleMaps.podspec --allow-warnings",
51-
"downleveldts": "pnpm downlevel-dts dist/typings dist/typings/ts_old --to=3.5",
52-
"pack-local": "pnpm run build && pnpm pack && find . -name 'capacitor-google-maps-*tgz' -exec bash -c 'mv $0 capacitor-google-maps.tgz' {} \\; ",
53-
"unittest:ios": "xcodebuild test -project ./unit-tests/ios/GoogleMapsPlugin/GoogleMapsPlugin.xcodeproj -scheme TestApp -destination 'platform=iOS Simulator,name=iPhone 12,OS=15.2' | xcpretty && exit ${PIPESTATUS[0]}",
54-
"unittest:android": "cd ./unit-tests/android && ./gradlew testDebugUnitTest"
44+
"prepublishOnly": "npm run build",
45+
"publish:cocoapod": "pod trunk push ./Typical.podspec --allow-warnings"
5546
},
5647
"devDependencies": {
5748
"@capacitor/android": "next",
49+
"@capacitor/cli": "^6.0.0",
5850
"@capacitor/core": "next",
59-
"@capacitor/docgen": "0.3.0",
51+
"@capacitor/docgen": "0.2.2",
6052
"@capacitor/ios": "next",
61-
"@ionic/prettier-config": "^1.0.1",
62-
"@types/resize-observer-browser": "^0.1.7",
63-
"@types/supercluster": "^7.1.0",
64-
"@typescript-eslint/eslint-plugin": "^5.59.2",
65-
"@typescript-eslint/parser": "^5.59.2",
66-
"downlevel-dts": "^0.7.0",
53+
"@ionic/eslint-config": "^0.4.0",
54+
"@ionic/prettier-config": "~1.0.1",
55+
"@ionic/swiftlint-config": "^1.1.2",
6756
"eslint": "^8.57.0",
68-
"eslint-config-prettier": "^8.8.0",
69-
"eslint-plugin-import": "^2.25.4",
70-
"prettier": "^2.8.8",
71-
"prettier-plugin-java": "~2.1.0",
72-
"rimraf": "^3.0.2",
73-
"rollup": "^2.78.1",
74-
"swiftlint": "^1.0.2",
75-
"typescript": "^5.4.2"
57+
"prettier": "~2.3.0",
58+
"prettier-plugin-java": "~1.0.2",
59+
"rimraf": "^6.0.1",
60+
"rollup": "^4.26.0",
61+
"swiftlint": "^1.0.1",
62+
"typescript": "~4.1.5"
7663
},
7764
"peerDependencies": {
7865
"@capacitor/core": ">=7.0.0"
7966
},
67+
"prettier": "@ionic/prettier-config",
68+
"swiftlint": "@ionic/swiftlint-config",
69+
"eslintConfig": {
70+
"extends": "@ionic/eslint-config/recommended"
71+
},
8072
"capacitor": {
8173
"ios": {
8274
"src": "ios"
@@ -87,10 +79,5 @@
8779
},
8880
"publishConfig": {
8981
"access": "public"
90-
},
91-
"dependencies": {
92-
"@googlemaps/js-api-loader": "~1.16.8",
93-
"@googlemaps/markerclusterer": "~2.5.3",
94-
"@types/google.maps": "~3.58.1"
9582
}
9683
}

0 commit comments

Comments
 (0)