1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.createBuildSourceFile = createBuildSourceFile;
7exports.withBuildSourceFile = void 0;
8function _fs() {
9  const data = _interopRequireDefault(require("fs"));
10  _fs = function () {
11    return data;
12  };
13  return data;
14}
15function _path() {
16  const data = _interopRequireDefault(require("path"));
17  _path = function () {
18    return data;
19  };
20  return data;
21}
22function _iosPlugins() {
23  const data = require("../plugins/ios-plugins");
24  _iosPlugins = function () {
25    return data;
26  };
27  return data;
28}
29function _Xcodeproj() {
30  const data = require("./utils/Xcodeproj");
31  _Xcodeproj = function () {
32    return data;
33  };
34  return data;
35}
36function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
37/**
38 * Create a build source file and link it to Xcode.
39 *
40 * @param config
41 * @param props.filePath relative to the build source folder. ex: `ViewController.swift` would be created in `ios/myapp/ViewController.swift`.
42 * @param props.contents file contents to write.
43 * @param props.overwrite should the contents overwrite any existing file in the same location on disk.
44 * @returns
45 */
46const withBuildSourceFile = (config, {
47  filePath,
48  contents,
49  overwrite
50}) => {
51  return (0, _iosPlugins().withXcodeProject)(config, config => {
52    const projectName = (0, _Xcodeproj().getProjectName)(config.modRequest.projectRoot);
53    config.modResults = createBuildSourceFile({
54      project: config.modResults,
55      nativeProjectRoot: config.modRequest.platformProjectRoot,
56      fileContents: contents,
57      filePath: _path().default.join(projectName, filePath),
58      overwrite
59    });
60    return config;
61  });
62};
63
64/**
65 * Add a source file to the Xcode project and write it to the file system.
66 *
67 * @param nativeProjectRoot absolute path to the native app root `user/app/ios`
68 * @param filePath path relative to the `nativeProjectRoot` for the file to create `user/app/ios/myapp/foobar.swift`
69 * @param fileContents string file contents to write to the `filePath`
70 * @param overwrite should write file even if one already exists
71 */
72exports.withBuildSourceFile = withBuildSourceFile;
73function createBuildSourceFile({
74  project,
75  nativeProjectRoot,
76  filePath,
77  fileContents,
78  overwrite
79}) {
80  const absoluteFilePath = _path().default.join(nativeProjectRoot, filePath);
81  if (overwrite || !_fs().default.existsSync(absoluteFilePath)) {
82    // Create the file
83    _fs().default.writeFileSync(absoluteFilePath, fileContents, 'utf8');
84  }
85
86  // `myapp`
87  const groupName = _path().default.dirname(filePath);
88
89  // Ensure the file is linked with Xcode resource files
90  if (!project.hasFile(filePath)) {
91    project = (0, _Xcodeproj().addBuildSourceFileToGroup)({
92      filepath: filePath,
93      groupName,
94      project
95    });
96  }
97  return project;
98}
99//# sourceMappingURL=XcodeProjectFile.js.map